2

I am using ASP.NET MVC 5 and I have a form that uses the following Html helper

   @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })

When I look at the rendered Html that is produced in the browser it seems to render this as a Span element. This causes the validation message to be on the same line as one of my Labels.

What I want to know is how to make the @Html.ValidationMessageFor() method render a Div element instead of a Span?

2
  • 2
    Wrap the @Html.ValidationMessageFor() in a div like this -> <div>@Html.ValidationMessageFor()</div>. Or you can make a HTML extension method that will wrap the span into a div, or use css. Commented May 26, 2016 at 8:54
  • 1
    Just use css to style it Commented May 26, 2016 at 8:55

3 Answers 3

6

Add a parameter at last.

@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger newLine" },"div")

It will create a div.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This really answers the question "How to change SPAN to DIV"
4

Instead of turning it into a div just add a style rule do the ValidationMessageFor:

 @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger newLine" })

.newLine 
{
    display:block;
}

Comments

1

Inside App_Start, where you can activate client-side-validation and unobtrusive, you can set the default element for the validation.

HtmlHelper.ClientValidationEnabled = true;
HtmlHelper.UnobtrusiveJavaScriptEnabled = true;
HtmlHelper.ValidationMessageElement = "div";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.