How to show the required fields on the first access of a View?
I am using MVC 2.0 and I wonder know how to show my required fields on the view on first acess.
For example.. I have a page that I can register a person and also I have my fields:
But, just "Name" and "Phone" fields are requireds. I alread have the validation in my server side, but, I want that when somebody load the page, the page shows the required fields.
For example: Name [Put_text_here___________] *
I have on my pages:
<%= Html.TextBoxFor(model => model.soliInscricaoImobiliaria})%> <%= Html.ValidationMessageFor(model => model.Name, "*") %>
How do I do that?
Best regards, Dan
This is not super user-friendly, and I'd look at putting it into a @helper block. But short of simply hard-coding an asterisk into the form...
@(ViewData.ModelMetadata.Properties.First(m => m.PropertyName == "Name").IsRequired ? "*" : "")
More information here:
http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-2-modelmetadata.html http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-4-custom-object-templates.html