【问题标题】:Using MVC3 html helpers break validation messages使用 MVC3 html 助手破坏验证消息
【发布时间】:2011-08-09 15:42:39
【问题描述】:
    private static IDictionary<string, object> CreateBindAttribute<TModel, TProperty>(Expression<Func<TModel, TProperty>> expression)
            {
                string exp = ExpressionHelper.GetExpressionText(expression);
                var htmlAttributes = new Dictionary<string, object>();

                // knockout uses data-bind to bind the UI to the viewModels
                htmlAttributes.Add("data-bind", "value: " + exp + ", uniqueName: true");

                return htmlAttributes;
            }

public static MvcHtmlString KoPasswordFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            //return htmlHelper.PasswordFor(expression, CreateBindAttribute(expression));
            return htmlHelper.PasswordFor(expression,);
        }

上面的代码突出了我的问题。 我有一个 knockoutJS html 助手,它向标记添加了“数据绑定”属性。但是,我发现该字段上的验证消息不起作用。

如果我评论呈现绑定属性的重载,则验证消息显示正常。请注意,验证仍然适用于注释掉的行,但下面的验证文本不显示。

谁能确认这种行为,如果可以,如何解决?

【问题讨论】:

    标签: jquery asp.net-mvc-3 knockout.js


    【解决方案1】:

    这有点猜测,但我认为 uniqueName 是问题所在。当您打开不显眼的验证时,Html 助手默认呈现 uniqueId/Names。如果不显眼的验证绑定到那个,然后敲除的 uniqueName 正在更改名称,这将破坏验证。

    删除唯一名称,看看它是否有效。

    【讨论】:

      【解决方案2】:

      在你的css中添加“white-space:pre-word !important”或用“”包装你的Html.ValidationMessageFor() 在您的验证器消息中使用“\n”来中断消息

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-01-20
        • 2011-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-23
        • 1970-01-01
        相关资源
        最近更新 更多