【问题标题】:.Net MVC 3 Razor example of using DdUovFor workaround to fix DropDownListFor validation?.Net MVC 3 Razor 使用 DdUovFor 解决方法修复 DropDownListFor 验证的示例?
【发布时间】:2012-01-11 20:23:54
【问题描述】:

我在this post 中遇到了以下代码,它应该修复 DropDownListFor 的验证。但是,我不确定要从视图中为参数传递什么值:

this HtmlHelper<TModel> htmlHelper

应该为这个值传递什么?你能提供一个在视图中使用它的例子吗?这与未提供示例的this question 有关。

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

【问题讨论】:

    标签: c# asp.net-mvc-3 validation razor drop-down-menu


    【解决方案1】:

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

    查看扩展方法: http://msdn.microsoft.com/en-us/library/bb383977.aspx

    htmlHelper 是 @Html.. 中的 Html 实例,它在 View 中自动引用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 2011-08-05
      • 1970-01-01
      • 2013-07-12
      • 1970-01-01
      • 2017-12-10
      相关资源
      最近更新 更多