0

I am searching for a while solution, but I can not found it. I have this ValidateMessageFor in my Razor, which show error message if it cames to there.

Now I have create this css frame for this message and I whant to it be displayed only if there is some ValidateMessage.

I have try this:

@{
    if (@Html.ValidationMessageFor(u => u.CustomType) != null)
    {
    <p class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>@Html.Raw(@Html.ValidationMessageFor(u => u.CustomType))
    </p>
    }
}

But this is not working. Problem is that frame is always displayed (inside is no error message, until I make mistake, and then error message displays). This is how it looks, when I come to this form:

enter image description here

If error shows:

enter image description here

tereško
  • 58,060
  • 25
  • 98
  • 150
DaniKR
  • 2,418
  • 10
  • 39
  • 48
  • 1
    You can write your own validationMessageHelper : http://stackoverflow.com/questions/7892174/mvc3-extension-for-validatormessage – AliRıza Adıyahşi Aug 05 '14 at 11:53
  • ValidationMessage works, the problem is, that I whant to show it in my class if error displays. Curent code shows my css (without error message) frame all the time – DaniKR Aug 05 '14 at 11:56

1 Answers1

1

Try this: @Html.Raw(Server.HtmlDecode(@Html.ValidationMessageFor(u => u.CustomType).ToString())))

full code:

@{
    if (!String.IsNullOrEmpty(@Html.ValidationMessageFor(u => u.CustomType).ToString()))
    {
    <p class="alert alert-danger alert-dismissible" role="alert">
        <button type="button" class="close" data-dismiss="alert">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
        </button>
        @Html.Raw(Server.HtmlDecode(@Html.ValidationMessageFor(u => u.CustomType).ToString())))
    </p>
    }
}
AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197