gpt4 book ai didi

asp.net-mvc - ASP.NET MVC UpdateModel 抛出异常 : "Model could not be updated"

转载 作者:行者123 更新时间:2023-12-03 23:33:01 26 4
gpt4 key购买 nike

我试图在 MVC 中更新一个简单的模型,但它不起作用,它抛出一个异常,说模型无法更新:

      [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)
{
[...]
}
[...]
}

这是模型:
[Required(ErrorMessage="**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; }
}

我如何使用这种方法“UpdateModel”?

最佳答案

也许您的数据与验证不匹配。

我会尝试 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.

关于asp.net-mvc - ASP.NET MVC UpdateModel 抛出异常 : "Model could not be updated",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4042940/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com