2012-05-29 28 views
2

這個Html.DropDownListFor助手應該產生數據屬性,但是如果有嵌套的話,將會失敗。因此,這不能產生他們:任何方法不必追溯生成這些數據屬性?

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

我怎樣才能避免回去定義數據 - 一些腳本手動屬性缺失?

+0

相似:http://stackoverflow.com/q/4881583/1026459 –

回答

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); 
    } 
相關問題