jaffa
jaffa

Reputation: 27350

Using MVC3 html helpers break validation messages

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

The above code highlights my problem. I have a knockoutJS html helper which adds 'data-bind' attribute to the markup. However, I find that the validation messages on the field don't work.

If I comment the overload which renders the bind attribute, then the validation messages show up ok. Note that the validation still works with the commented out line, but the validation text below doesn't show.

Can anyone confirm this behaviour and if so how to fix it?

Upvotes: 1

Views: 504

Answers (2)

Alex Filipovik
Alex Filipovik

Reputation: 21

in your css add "white-space:pre-word !important" or wrap your Html.ValidationMessageFor() with "" in your validator message use "\n" to break the message

Upvotes: 2

madcapnmckay
madcapnmckay

Reputation: 15984

This is a bit of a guess but I would think that the uniqueName is the problem. When you have unobtrusive validation turned on the Html helpers render uniqueId/Names by default. If the unobtrusive validation is binding to that and then the uniqueName of knockout is changing the name, this would break the validation.

Remove the unique name and see if it works.

Upvotes: 0

Related Questions