【发布时间】:2011-01-26 08:19:45
【问题描述】:
在 ASP.NET MVC 中是否允许更改提交的值?
[HttpPost]
public ActionResult Create(Person toCreate)
{
toCreate.Lastname = toCreate.Lastname + "-A-";
return View(toCreate);
}
我尝试了该代码,但 ASP.NET MVC 一直显示用户提交的值
[更新]
这个:
[HttpPost]
public ActionResult Create(Person toCreate)
{
return View(new Person { Lastname = "Lennon" });
}
或者这个:
[HttpPost]
public ActionResult Create(Person toCreate)
{
return View();
}
仍然显示用户输入的值,这让我想到,为什么生成的代码需要在HttpPost中发出:return View(toCreate)? 为什么不直接返回 View()? 至少它不会违反控制器可以覆盖值的预期
[更新:2010-06-29]
在这里找到答案:ASP.NET MVC : Changing model's properties on postback 和这里:Setting ModelState values in custom model binder
工作代码:
[HttpPost]
public ActionResult Create(Person toCreate)
{
ModelState.Remove("Lastname");
toCreate.Lastname = toCreate.Lastname + "-A-";
return View(toCreate);
}
【问题讨论】:
-
发帖后不应该重定向吗?
标签: asp.net-mvc