Asp.net mvc 3 在mvc3中使用javascript进行自定义验证
我是MVC3的新手。例如,我在自定义验证方面有问题 在我的BasicInfoViewModel.cs中Asp.net mvc 3 在mvc3中使用javascript进行自定义验证,asp.net-mvc-3,validation,Asp.net Mvc 3,Validation,我是MVC3的新手。例如,我在自定义验证方面有问题 在我的BasicInfoViewModel.cs中 [Required] [Display(Name = "State", ResourceType = typeof(Resources.Global))] public string State { get; set; } [Display(Name = "City", ResourceType = typeof(Resources.Global))] pub
[Required]
[Display(Name = "State", ResourceType = typeof(Resources.Global))]
public string State { get; set; }
[Display(Name = "City", ResourceType = typeof(Resources.Global))]
public string City { get; set; }
在我的BasicDetailsView.cshtml中
<label>
<span class="td">@Application.Resources.Global.State</span>
@Html.DropDownListFor(m => m.State, (List<SelectListItem>)ViewData["State"])
</label>
<label>
<span class="td">@Application.Resources.Global.City</span>
@Html.DropDownListFor(m => m.City, (List<SelectListItem>)ViewData["City"])
</label>
@Application.Resources.Global.State
@DropDownListFor(m=>m.State,(List)ViewData[“State”])
@应用程序、资源、全球、城市
@DropDownListFor(m=>m.City,(List)ViewData[“City”])
如果state属性返回true,则只需要“City”。如果不是,则不需要城市,则文本框应被禁用。
我没有使用EditorFor,也没有使用DropDownListFor,因为我使用的是纯html。有人能帮我解决这个问题吗?
感谢……是一组验证数据注释,它们扩展了现有的注释并提供了附加功能。例如,此包中的[RequiredIfNotEmpty]
属性非常适合您的场景,因为它允许条件验证
[Display(Name = "State", ResourceType = typeof(Resources.Global))]
public string State { get; set; }
[RequiredIfNotEmpty("State")]
[Display(Name = "City", ResourceType = typeof(Resources.Global))]
public string City { get; set; }
现在State属性是可选的。但是如果它有一些值,则需要城市属性。您可能需要查看。要禁用城市下拉列表,请使用jquery。要检查数据是否有效,可以使用js方法$(“selector”).valid(),它返回0或1,还显示指定字段的验证消息?我在您显示的代码中看不到它。我所能看到的只是两个下拉列表(顺便说一下,这是错误的,因为您对值和项使用了相同的属性名)。@DarinDimitrov,更正。我们如何才能做到这一点?@DarinDimitrov my ViewData[“city”]有一组SelectItem,每个SelectItem都有不同的id和值。但是,您不应该使用
city
作为下拉列表的第一个参数。或者将ViewData中的值重命名为ViewData[“cities”]
。