ASP.NET MVC UpdateModel throws exception: "Model could not be updated"
Im trying to update a simple model in MVC,but its not working,it throws an exception saying that the Model could not be updated:
[HttpPost]
public ActionResult SignIn([Bind(Exclude="TxtEmail")]Usuarios usuario,FormCollection fc)
{
try
{
UsuariosModel userModel = new UsuariosModel(usuario);
userModel.Usuarios.TxtEmail = "test@test.com";
UpdateModel(userModel);
if (ModelState.IsValid)
{
[...]
}
[...]
}
This is the model:
[Required(Err开发者_运维知识库orMessage="**O email é requerido")]
[RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$",ErrorMessage="**Email Inválido")]
public string TxtEmail
{
get { return this.txt_email; }
set { this.txt_email = value; }
}
How can i use this method "UpdateModel"?
Maybe your data does not match the validation.
I would try TryUpdateModel.
The TryUpdateModel method is like the UpdateModel method except that the TryUpdateModel method does not throw an InvalidOperationException exception if the updated model state is not valid.
Look in your ModelState entries ( accessible with this.ModelState
).
ModelState contains an entry for each property and the errors for that property in the model you are trying to bind. Chances are you are passing the wrong datatype along in the post or get action.
Use:
UpdateModel<UsuariosModel>(userModel);
I hope this will solve the problem.
精彩评论