asp.net-mvc-3 - 有什么方法可以不必追溯生成这些数据属性?

标签 asp.net-mvc-3 razor html-helper html.dropdownlistfor

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 
 ) 
)

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

最佳答案

在使用 @Html.DropDownListFor 帮助器时,除了追溯分配这些缺失的数据属性之外,没有其他办法。需要保留的重要内容是 data-val = "true"data-val-required="This value is required"。此外,使用 data-valmsg-replace="true"data-valmsg-for="ID OF SELECT ELEMENT"class= 进行验证的跨度“字段验证有效”

但是,如果您选择使用自定义助手,则可以避免这种情况。这可以在这里看到: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);
    }

关于asp.net-mvc-3 - 有什么方法可以不必追溯生成这些数据属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10806860/

相关文章:

json - 从 RavenDB 获取 JSON 数据

.net - 今年 ASP.NET MVC 3

c# - 发生 Razor 错误时,未创建位于的 View

razor - RazorEngine 中的模板是如何缓存的?

asp.net-mvc - MVC3 表单过帐值

jquery - 将悬停状态更改应用于子元素

asp.net-mvc-3 - EF4.3知道上下文是否已成功插入实体

asp.net-mvc - 在 ASP.NET MVC 中使用描述元数据属性

asp.net-mvc - 为什么.Net MVC 中没有 Html.CheckBoxFor() 方法?

c# - 在 Html Helper 中创建带有分隔符的选择列表