I have a model class that has a couple of required fields:
public class UserMetadata
{
[Required(ErrorMessage = "Please enter a name.")]
public string Name { get; set; }
[Required(ErrorMessage = "Please enter a password.")]
public string Password { get; set; }
}
On the create view, if I don't give a name and/or password, then the validation summary errors appears. All nice and good. For the edit view, I'm only displaying the 'Name' field - I don't to show the 'Password' field.
When I save my changes on the edit page, the validation summary error appears saying that I must enter a password.
How can I control the validation of the password field, so that for the edit view, it should not bother with it? Or, am I approaching this the wrong way? I still want the 'Name' field validation to work on the edit view.
EDIT:
For my MVC project, I'm using Entity Framework. Thus, I have a 'UserMetadata' class defined so that I can attached things like '[Required]' onto certain fields on the 'User' class (which is in the EDMX file).
I should also explain that I'm using a view model eg 'UserEditViewModel' which has a property 'User' attached to it. So on my post:
[HttpPost]
public ActionResult Edit(UserEditViewModel inputViewModel)
{
if(ModelState.IsValid) { inputViewModel.User blah.... }
}
Think I rushed a bit when typing this question. Any other missing information you think is important, then please give me a shout.
Cheers. Jas.