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; } }
In the create view, if I do not specify a name and / or password, summary validation errors will appear. All is well and good. To represent editing, I only show the "Name" field - I should not show the "Password" field.
When I save my changes to the edit page, a summary validation error appears indicating that I must enter a password.
How can I control the validation of a password field so that it doesnβt have to worry about it to represent editing? Or am I getting it wrong? I still want the "Name" field check to work on the edit view.
EDIT:
In my MVC project, I am using Entity Framework. So I have a specific UserMetadata class so that I can attach things like "[Required]" to certain fields of the User class (which is in the EDMX file).
I must also explain that I am using a view model, such as "UserEditViewModel", to which the "User" property is attached. So, in my post:
[HttpPost] public ActionResult Edit(UserEditViewModel inputViewModel) { if(ModelState.IsValid) { inputViewModel.User blah.... } }
I think I was a little hasty, typing this question. Any other missing information that you consider important, then please give me a shout.
Greetings. Jac.
source share