I want to display an asterisk (*) next to a text box in my form when initially displayed (GET) Also I want to use the same view for GET/POST when errors are present) so For the GET request I pass in an empty model such as return View(new Person());
Later, when the form is submitted (POST), I use the data annotations, check the model state and display the errors if any Html.ValidationMessageFor(v => v.FirstName)
For GET request, the model state is valid and no messages, so no asterisk gets displayed. I am trying to workaround this by checking the request type and just print asterisk. @(HttpContext.Current.Request.HttpMethod == "GET"? "*" : Html.ValidationMessageFor(v=> v.FirstName).ToString())
The problem is that Html.ValidationMessageFor(v=> v.FirstName).ToString() is already encoded and I want to get the raw html from Html.ValidationMessageFor(v=> v.FirstName)
Or may be there is a better way here. 1. How do you display default helpful messages (next to form fields) - such as "Please enter IP address in the nnn.nnn.nnn.nnn format) for GET requests and then display the errors if any for the post? 2. What is the best way from a razor perspective to check an if condition and write a string or the MvcHtmlString