at the end? I check about 10 input fields on the form. ValidationMessageFor ta...">

MVC3 - How to insert ValidationMessageFor with line break <br/"> at the end?

I check about 10 input fields on the form. ValidationMessageFor tags should be at the top of the page, so I write each, for example:

@Html.ValidationMessageFor(model => model.Customer.ADDRESS.NAME)
@Html.ValidationMessageFor(model => model.Customer.ADDRESS.CITY)

etc. My models are as follows:

[Required(ErrorMessage = Constants.ErrorMsgNameMissing)]
public string NAME { get; set; }
[Required(ErrorMessage = Constants.ErrorMsgCityMissing)]
public string CITY { get; set; }

Constants are strings. Now, if more than one ValidationMessageFor is shown, they are all on the same line. How can I insert a line break, for example <br />at the end of each message?

This is NOT the right way:

@Html.ValidationMessageFor(model => model.Customer.ADDRESS.NAME)<br />
@Html.ValidationMessageFor(model => model.Customer.ADDRESS.CITY)<br />

as displayed <br />even if there is no error ...;)

Thanks in advance.

PS: Displaying them as a list will also be great.

+5
source share
5 answers

I think you are really looking for:

@Html.ValidationSummary()
+3

@Html.ValidationSummary(),

+1

ValidationSummary Asp.net MVC 3. , , , . ValidationMessageFor.

+1

, :

@Html.ValidationSummary(/*...*/)

.

, , @Html.ValidationMessageFor(/*...*/) , CSS <br />

+1

I'm not sure if there is a built-in way, but you can easily create another extension method:

public static MvcHtmlString ValidationMessageLineFor<TModel, TProperty>(this HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> expression) {
    return helper.ValidationMessageFor(expression) + "<br />";
}

Something like that?

+1
source

All Articles