TryUpdateModel 在属性值更改后不更新对象
我正在使用 MVC 2 和 EF4。我有一个显示我的应用程序(类)属性的视图。并非所有属性都显示在视图中。单击提交按钮后,需要设置几个属性。
我正在通过客户端验证,但我的服务器验证仍然失败。我在 CreateApplication
操作中收到一个 Application
对象,更新属性,并执行 ModelState.IsValid
检查。这仍然是错误的。我循环浏览了错误列表,它显示了我使用必需数据注释在 SubmitterEmployeeNumber
属性上设置的错误文本。我确实设置了它并且更新了我的模型,但验证仍然失败。这是我的代码:
[HttpPost]
public ActionResult CreateApplication(Application application)
{
application.SubmitterEmployeeNumber = "123456";
TryUpdateModel(application);
if (ModelState.IsValid)
{
}
}
这是我显示视图的方式:
public ActionResult CreateApplication()
{
var viewModel = new ApplicationViewModel(new Application(), db.AccountTypes);
return View(viewModel);
}
绑定后设置属性后如何让验证通过?
UpdateModel
和 TryUpdateModel
之间有什么区别?何时需要使用它们?
编辑:
我将操作的名称更改为:
[HttpPost]
public ActionResult CreateApp()
{
var application = new Application
{
ApplicationStateID = 1,
SubmitterEmployeeNumber = "123456"
};
if (TryUpdateModel(application))
{
int success = 0;
}
}
这是我的观点:
<% using (Html.BeginForm("CreateApp", "Application")) {%>
TryUpdateModel
仍然验证为 false。我输入 int success = 0;
只是为了看看它是否会进入其中,但它不会。
I'm using MVC 2 and EF4. I have a view that displays my Application (class) properties. Not all the properties are displayed in the view. There are a couple of the properties that need to be set once the submit button is clicked.
I'm getting client validation to pass, but my server validation is still failing. I receive an Application
object in my CreateApplication
action, I update a property, and do a ModelState.IsValid
check. It is still false. I did a loop through my errors list and it displays the error text that I set on my SubmitterEmployeeNumber
property using a Required data annotation. I did set it and I did update my model, but validation is still failing. Here is my code:
[HttpPost]
public ActionResult CreateApplication(Application application)
{
application.SubmitterEmployeeNumber = "123456";
TryUpdateModel(application);
if (ModelState.IsValid)
{
}
}
Here is how I display the view:
public ActionResult CreateApplication()
{
var viewModel = new ApplicationViewModel(new Application(), db.AccountTypes);
return View(viewModel);
}
How do I get the validation to pass after I set the property after binding?
What is the difference between UpdateModel
and TryUpdateModel
and when do I need to use each?
EDIT:
I changed the name of the action to:
[HttpPost]
public ActionResult CreateApp()
{
var application = new Application
{
ApplicationStateID = 1,
SubmitterEmployeeNumber = "123456"
};
if (TryUpdateModel(application))
{
int success = 0;
}
}
Here is my view:
<% using (Html.BeginForm("CreateApp", "Application")) {%>
TryUpdateModel
still validates as false. I put in int success = 0;
just to see if it will go into it but it doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新:由于评论部分存在许多混乱,这里有一个完整的工作示例。
模型:
控制器:
视图:
希望这能解决问题。
UPDATE: Due to many confusions in the comments section here's a full working example.
Model:
Controller:
View:
Hope this clears things up.