Ignore Required attribute when saving to edit page

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.

+6
source share
3 answers

I ended up doing this in my action method:

 ModelState.Remove("User.Password"); 

Now my code works fine, only raising validation errors in the "Name" field, and this is what I wanted.

+10
source

ModelState.Remove("User.Password") did not work for me in MVC 3.

However, it worked.

Option 1:

 ModelState.Remove("Password") 

Option 2:

 ModelState.Where(m => m.Key == "Password").FirstOrDefault().Value.Errors.Clear() 
+4
source

Assuming you are using the UserMetadata class as the presentation model, you should use a different presentation model on the page (presentation).

eg.

 public class UserMetaDataCreate { [Required(ErrorMessage = "Please enter a name.")] public string Name { get; set; } [Required(ErrorMessage = "Please enter a password.")] public string Password { get; set; } } 

and UserMetaDataEdit

 public class UserMetaDataEdit { [Required(ErrorMessage = "Please enter a name.")] public string Name { get; set; } } 

In principle, if viewing the edit does not require a password, it should not be in the model in any case.

In your controller

 public ActionResult Create() { return View(new UserMetaDataCreate()); } // and subsequent post actions [HttpPost] public ActionResult Edit(UserMetaDataEdit vm) { if(ModelState.IsValid) { // do something } else return View(vm); } 

Of course, you can find some inheritance, as your models will become more complex, for example.

 public class UserMetaData { [Required(ErrorMessage = "Please enter a name.")] public string Name { get; set; } } 

And a subclass of your view models

 public class UserMetaDataEdit { [Required(ErrorMessage = "Please enter a password.")] public string Password { get; set; } } public class UserMetaDataCreate { } 

But I'm not sure if this makes sense contextually, since UserMetaData semantically includes a password.

0
source

Source: https://habr.com/ru/post/887387/


All Articles