.Net MVC 3 Razor使用DdUovFor解决方法修复DropDownListFor验证的示例

本文关键字:DropDownListFor 验证 方法 MVC Razor 使用 解决 DdUovFor Net | 更新日期: 2023-09-27 18:19:58

我在这篇文章中看到了以下代码,该代码旨在修复DropDownListFor的验证。然而,我不确定从视图中传递参数的值:

this HtmlHelper<TModel> htmlHelper

这个值应该传递什么?你能提供一个在视图中使用这个的例子吗?这与这个没有提供例子的问题有关。

 [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);
        }

.Net MVC 3 Razor使用DdUovFor解决方法修复DropDownListFor验证的示例

您不需要传递任何信息,只需调用@Html.DdUovFor即可忽略该参数。

请参阅扩展方法:http://msdn.microsoft.com/en-us/library/bb383977.aspx

htmlHelper是@Html中Html的实例。。它在视图中自动引用。