gpt4 book ai didi

asp.net-mvc - 防止验证 asp.net MVC 3 中的表单的一部分

转载 作者:行者123 更新时间:2023-12-01 05:42:01 24 4
gpt4 key购买 nike

我有一个表单,其中包含 11 个用于送货地址的字段和用于帐单地址的 11 个字段以及一个名为“送货地址与帐单地址相同”的复选框。选中该框后,我使用 jquery 在包含送货地址输入的 div 上调用 .hide() 。然后如何禁用对送货地址字段的客户端和服务器端验证?

最佳答案

在MVC4(或3)中为RequiredIf属性尝试此解决方案可在

http://cchitsiang.blogspot.com/2011/04/requiredif-conditional-validation-if.html

可以允许用户根据选中的复选框提交不完整的表单或完全验证的表单。确保在 RequiredIfAttribute.cs 中更改以下内容

[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]

引用上面提到的链接,

对于要验证的下拉列表,请确保在 model.cs 文件中执行以下操作

在所有变量的底部添加 IsCompleted bool 变量

    [Display(Name = "Completed")]
public bool IsCompleted { get; set; }

对于要验证的下拉列表,请在 Controller 内的 ViewBag 中传递 DBset,例如,
 ViewBag.Relationships = db.Relationships; 

在 View 中,添加如下下拉菜单
@Html.DropDownListFor(model => model.RelationshipId, new     SelectList(ViewBag.Relationships, "RelationshipId", "RelationshipName"), "--- Select ---")   <br />
@Html.ValidationMessageFor(model => model.RelationshipId)

关于asp.net-mvc - 防止验证 asp.net MVC 3 中的表单的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4927001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com