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

スクリプトを使用して、欠落しているデータ属性に戻って手動で定義する必要をなくすにはどうすればよいですか?

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 に答える