C# 如果在另一个下拉列表中选择美国作为国家,则验证州下拉列表
需要一些建议,我正在尝试编写一个验证器,它只在选择dropdownlist中的特定值时启动 我在这张表格上有两个下拉列表,一个是国家,另一个是美国各州,只有在从国家下拉列表中选择美国时,州下拉列表才会显示 我需要一个验证器,使国家dropdownlist列表只有当美国被选为一个国家的必填字段C# 如果在另一个下拉列表中选择美国作为国家,则验证州下拉列表,c#,jquery,asp.net-mvc-3,validation,html-select,C#,Jquery,Asp.net Mvc 3,Validation,Html Select,需要一些建议,我正在尝试编写一个验证器,它只在选择dropdownlist中的特定值时启动 我在这张表格上有两个下拉列表,一个是国家,另一个是美国各州,只有在从国家下拉列表中选择美国时,州下拉列表才会显示 我需要一个验证器,使国家dropdownlist列表只有当美国被选为一个国家的必填字段 作为背景信息,这是一个MVC3 Web应用程序,States dropdownlist的显示/隐藏代码是JQuery 您可以编写自定义验证属性: public class RequiredIfAttribu
作为背景信息,这是一个MVC3 Web应用程序,States dropdownlist的显示/隐藏代码是JQuery 您可以编写自定义验证属性:
public class RequiredIfAttribute : ValidationAttribute
{
public RequiredIfAttribute(string otherProperty, object otherPropertyValue)
{
OtherProperty = otherProperty;
OtherPropertyValue = otherPropertyValue;
}
public string OtherProperty { get; private set; }
public object OtherPropertyValue { get; private set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var property = validationContext.ObjectType.GetProperty(OtherProperty);
if (property == null)
{
return new ValidationResult(string.Format("Unknown property: {0}", OtherProperty));
}
object otherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);
if (!object.Equals(OtherPropertyValue, otherPropertyValue))
{
return null;
}
if (value != null)
{
return null;
}
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
}
现在您可以拥有一个视图模型:
public class MyViewModel
{
public string Country { get; set; }
[RequiredIf("Country", "usa", ErrorMessage = "Please select a state")]
public string State { get; set; }
public IEnumerable<SelectListItem> Countries
{
get
{
return new[]
{
new SelectListItem { Value = "fr", Text = "France" },
new SelectListItem { Value = "usa", Text = "United States" },
new SelectListItem { Value = "spa", Text = "Spain" },
};
}
}
public IEnumerable<SelectListItem> States
{
get
{
return new[]
{
new SelectListItem { Value = "al", Text = "Alabama" },
new SelectListItem { Value = "ak", Text = "Alaska" },
new SelectListItem { Value = "az", Text = "Arizona" },
};
}
}
}
还有一个观点:
@model MyViewModel
@using (Html.BeginForm())
{
<div>
@Html.DropDownListFor(x => x.Country, Model.Countries, "-- Country --")
@Html.ValidationMessageFor(x => x.Country)
</div>
<div>
@Html.DropDownListFor(x => x.State, Model.States, "-- State --")
@Html.ValidationMessageFor(x => x.State)
</div>
<button type="submit">OK</button>
}
@model MyViewModel
@使用(Html.BeginForm())
{
@DropDownListFor(x=>x.Country,Model.Countries,“--Country--”)
@Html.ValidationMessageFor(x=>x.Country)
@DropDownListFor(x=>x.State,Model.States,“--State-->”)
@Html.ValidationMessageFor(x=>x.State)
好啊
}
您可能还发现验证属性的定义很有用。另一种选择是将规则动态添加到jQuery中进行验证。 但是,您还需要在服务器端检查此自定义逻辑。 您可以在控制器中执行此操作,或者理想情况下,您的VieWModel将实现IValidTeableObject,以检查是否需要country=“usa”和county 使用jQuery的.rules.add和.remove: 所以你可以做一些事情,大致如下:
$(document).ready(function() {
$("#country").change(function(){
if($(this).val()=="usa")
{
$("#yourCountyDropDown").rules("add", {
required: true,
messages: {
required: "County is required"
}
});
}
else
{
$("#yourCountyDropDown").rules("remove");
}
});
});
对于您的ViewModel
public class WhateverYourObjectNameCreateViewModel : IValidatableObject
{
#region Validation
public IEnumerable Validate(ValidationContext validationContext)
{
if (this.Country=="USA" && string.IsNullOrEmpty(this.County))
{
yield return new ValidationResult("County is required");
}
}
#endregion
}
一直在尝试使用万无一失的Validation NuGet软件包向我的项目业务逻辑域项目中的Validation model State字段添加RequiredIf,还尝试使用JQuery验证DropDownList谢谢Darin,只有一个小问题,国家列表和州列表都来自数据库,另外,我已经有了视图模型,它可以从域模型中定义的两个不同表中提取值,理想情况下,需要在域模型中定义验证,并且有来自resources.resx文件的错误消息。如果我的错误当前已定义,是否值得为验证程序创建第二个视图模型,或者使用当前视图模型并编辑域模型(如果需要)?在所有情况下,您都需要使用视图模型。如果您已经有一些现有的视图模型,那么调整它,以便能够使用自定义验证器装饰相应的属性。就本地化错误消息而言,只需在自定义验证属性上使用
ErrorMessageResourceName
和ErrorMessageResourceType
属性,就像在标准必填属性上使用它们一样。我需要验证的必填字段不是国家,由于万事达卡处理it付款的方式,州是必填字段,国家字段已作为标准必填字段,如果选择的国家是美国,则需要检查是否已选择州对不起,很简单-我在国家=美国时验证了“县”,所以只需将“县”更改为“州”“。使用下面Darin方法的扩展,您也可以获得客户端验证-请参阅:编辑代码以验证状态,我所做的更改是否正确?已决定在我的ViewModel中进行验证,已将IEnumerable ValidationResult添加到我的ViewModel中如何使其在视图中工作?我可以像Darin的例子一样使用@Html.ValidationMessageFor吗?
public class WhateverYourObjectNameCreateViewModel : IValidatableObject
{
#region Validation
public IEnumerable Validate(ValidationContext validationContext)
{
if (this.Country=="USA" && string.IsNullOrEmpty(this.County))
{
yield return new ValidationResult("County is required");
}
}
#endregion
}