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:

  • Name;
  • Age;
  • City;
  • Country;
  • Phone;

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

Answers


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


Need Your Help

Porting Custom Install Actions to Wix

c# visual-studio-2010 wix wix3.7

I currently have a Visual Studio Deployment project for creating an MSI for my applicaiton, and I'm porting over to a WiX installer. The VS Installer used a library with Custom Install Actions that

Handling browser pop-up windows with Selenium

internet-explorer selenium popup regression-testing

We are running Selenium regression tests against our existing code base, and certain screens in our web app use pop-ups for intermediate steps.