2

这个Html.DropDownListFor助手应该产生 data- 属性但如果有任何嵌套则失败。所以这无法产生它们:

@( 
 Html.DropDownListFor( 
  m => m.P[0].CId, 
  new SelectList(
   Model.Cs.Values, 
   "Id", "DisplayFields", Model.Cs.StartValue), 
  Model.Cs.Message 
 ) 
)

但是,这会产生它们就好了:

@( 
 Html.DropDownListFor( 
  m => m.CId, 
  new SelectList(
   Model.Cs.Values, 
   "Id", "DisplayFields", Model.Cs.StartValue), 
  Model.Cs.Message 
 ) 
)

如何避免不得不返回并使用一些脚本手动定义丢失的数据属性?

4

1 回答 1

2

@Html.DropDownListFor在使用帮助程序时,除了追溯分配这些缺失的数据属性外,别无他法。要保留的重要的是data-val = "true", data-val-required="This value is required"data-valmsg-replace="true"此外,使用, data-valmsg-for="ID OF SELECT ELEMENT",进行验证的跨度class="field-validation-valid"

但是,如果您选择使用自定义助手,则可以避免这种情况。这可以在这里看到:http ://forums.asp.net/t/1649193.aspx/1/10答案详细说明了如何扩展 DropDownListFor 助手。这是他们使用的代码:

[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
    {
        return DdUovFor(htmlHelper, expression, selectList, null /* optionLabel */, null /* htmlAttributes */);
    }


    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, object htmlAttributes)
    {
        return DdUovFor(htmlHelper, expression, selectList, null /* optionLabel */, new RouteValueDictionary(htmlAttributes));
    }


    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, IDictionary<string, object> htmlAttributes)
    {
        return DdUovFor(htmlHelper, expression, selectList, null /* optionLabel */, htmlAttributes);
    }


    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel)
    {
        return DdUovFor(htmlHelper, expression, selectList, optionLabel, null /* htmlAttributes */);
    }


    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, object htmlAttributes)
    {
        return DdUovFor(htmlHelper, expression, selectList, optionLabel, new RouteValueDictionary(htmlAttributes));
    }


    [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Users cannot use anonymous methods with the LambdaExpression type")]
    [SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "This is an appropriate nesting of generic types")]
    public static MvcHtmlString DdUovFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string optionLabel, IDictionary<string, object> htmlAttributes)
    {
        if (expression == null)
        {
            throw new ArgumentNullException("expression");
        }


        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);


        IDictionary<string, object> validationAttributes = htmlHelper
            .GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);


        if (htmlAttributes == null)
            htmlAttributes = validationAttributes;
        else
            htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);


        return SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel, htmlAttributes);
    }
于 2012-05-29T22:49:29.097 回答