I have the Model (User) below, I use it to add new users and to update existing users. When I'm adding a new user it's required to enter the user name and the password, and when I'm updating it's required to enter only the user name because it's not allowed to change the password. Here is the problem, adding a new user everything works ok because I enter both name and password values so ModelState.IsValid returns true, but when updating an user there is no input to the password, so it always have the null value what makes the ModelState.IsValid returns false all the time. Is there a way to use the same model even if in the Add View the password is required and in the Update View it's not? please, any suggestions?
public class User {
public int ID { get; set; }
[Display(Name = "Nome do Usuário")]
[Required(ErrorMessage = "Digite o Nome do Usuário.")]
public string name { get; set; }
[Display(Name = "Senha")]
[Required(ErrorMessage = "Digite a Senha.")]
public string password { get; set; }
}
public ActionResult Add()
{
return View();
}
[HttpPost]
public ActionResult Add(User user)
{
UsuariosViewModel viewModel = new UsuariosViewModel();
if (ModelState.IsValid)
{
viewModel.Msg = new AdmUsuarios().CadastraUsuario(user);
}
return View(viewModel);
}
public ActionResult Update(int id)
{
UsuariosViewModel viewModel = new UsuariosViewModel();
using (var dataContext = new DBEntities())
{
Repository<User> repository = new Repository<User>(dataContext);
viewModel.User = repository.GetById(id);
}
return View(viewModel);
}
[HttpPost]
public ActionResult Update(User user, int id)
{
UsuariosViewModel viewModel = new UsuariosViewModel();
if (ModelState.IsValid)
{
viewModel.Msg = new AdmUsuarios().AlteraUsuario(user, id);
}
return View();
}
You should use view models.
Your data annotations would then belong on the view model being passed to the view
public class CreateViewModel
{
public int ID { get; set; }
[Display(Name = "Nome do Usuário")]
[Required(ErrorMessage = "Digite o Nome do Usuário.")]
public string name { get; set; }
[Display(Name = "Senha")]
[Required(ErrorMessage = "Digite a Senha.")]
public string password { get; set; }
}
and for edit
public class EditViewModel
{
public int ID { get; set; }
[Display(Name = "Nome do Usuário")]
[Required(ErrorMessage = "Digite o Nome do Usuário.")]
public string name { get; set; }
//perhaps you don't need the password at all in the edit view
}
Pass these classes to your view(s), not your domain model(User
), then, in the controller, map the view model properties back to the model before persisting to your data source.
I normally specify in my BaseViewModel
which elements to hide, then use jQuery to hide them. It can mess with your layout though, if you use more 'fancy' layouts than my usual top to bottom column-based layouts. Hide form-group
for each element to be hidden, and the elements below it just shift upwards.
My main line of work is LOB intranet apps, so I skip all the fancy most of the time.
来源:https://stackoverflow.com/questions/15142947/data-annotations-mvc3-required-attribute