ASP.Net MVC 3不显眼的客户端验证不适用于下拉列表 [英] ASP.Net MVC 3 unobtrusive client validation does not work with drop down lists
问题描述
我有一个简单的下拉列表,列表中的第一个项目有一个空值。如果我没有在列表中选择任何东西,客户端验证将忽略它。我使用注释属性在模型上根据需要设置该字段。
@ Html.DropDownListFor(model => Model。 CCPayment.State,UnitedStatesStates.StateSelectList)
[必需(ErrorMessage =状态为必需。)]
public string State
{
get
{
return _state;
}
set
{
_state = value;
}
}
任何想法?我是否缺少某些东西?
它看起来像一个合法的错误,这是我在搜索中找到的最好的解决方法: / p>
http://forums.asp.net /t/1649193.aspx
简而言之。您可以在自定义的Html扩展名中包含问题的来源 DropDownListFor
,并手动检索不引人注目的客户端验证规则,如下所示:
IDictionary< string,object> validationAttributes = htmlHelper。
GetUnobtrusiveValidationAttributes(
ExpressionHelper.GetExpressionText(expression),
元数据
);
然后,您将 validationAttributes
字典与任何其他html属性传递到您的自定义帮助器,并将其传递到 DropDownListFor
我完整的代码使用(我也有一个标签,你可以随意脱离):
public static IHtmlString DropDownListWithLabelFor< TModel ,TProperty>(此HtmlHelper< TModel>帮助器,表达式,字符串标签,IEnumerable< SelectListItem>项,字符串blankOption,对象htmlAttributes = null)
{
var l = new TagBuilder(label);
var br = new TagBuilder(br);
var metadata = ModelMetadata.FromLambdaExpression(expression,helper.ViewData);
var mergedAttributes = helper.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression),metadata);
if(htmlAttributes!= null)
{
foreach(TypeDescriptor.GetProperties(htmlAttributes)中的PropertyDescriptor描述符)
{
对象值=描述符。的GetValue(htmlAttributes);
mergedAttributes.Add(descriptor.Name,value);
}
}
l.InnerHtml = label + br.ToString(TagRenderMode.SelfClosing)+ helper.DropDownListFor(expression,items,blankOption,mergedAttributes);
return MvcHtmlString.Create(l.ToString(TagRenderMode.Normal));
}
I have a simple drop down list, the first item in the list has an empty value. If I do not select anything in the list the client validation ignores it. I have that field set up as required on the model using annotation attributes.
@Html.DropDownListFor(model => Model.CCPayment.State, UnitedStatesStates.StateSelectList)
[Required(ErrorMessage = "State is Required.")]
public string State
{
get
{
return _state;
}
set
{
_state = value;
}
}
any ideas? am I missing something?
It looks like a legitimate bug, here's the best workaround I've found in my search:
http://forums.asp.net/t/1649193.aspx
In short. You wrap the source of the problem, DropDownListFor
, in a custom Html extension and you manually retrieve the unobtrusive clientside validation rules like this:
IDictionary<string, object> validationAttributes = htmlHelper.
GetUnobtrusiveValidationAttributes(
ExpressionHelper.GetExpressionText(expression),
metadata
);
Then you combine your validationAttributes
dictionary with any other html attributes passed into your custom helper and you pass that along to DropDownListFor
The complete code that I'm using (I have a label in there too, you can feel free to de-couple):
public static IHtmlString DropDownListWithLabelFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression, string label, IEnumerable<SelectListItem> items, string blankOption, object htmlAttributes = null)
{
var l = new TagBuilder("label");
var br = new TagBuilder("br");
var metadata = ModelMetadata.FromLambdaExpression(expression, helper.ViewData);
var mergedAttributes = helper.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);
if (htmlAttributes != null)
{
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(htmlAttributes))
{
object value = descriptor.GetValue(htmlAttributes);
mergedAttributes.Add(descriptor.Name, value);
}
}
l.InnerHtml = label + br.ToString(TagRenderMode.SelfClosing) + helper.DropDownListFor(expression, items, blankOption, mergedAttributes);
return MvcHtmlString.Create(l.ToString(TagRenderMode.Normal));
}
这篇关于ASP.Net MVC 3不显眼的客户端验证不适用于下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!