Asp.net mvc 具有Submit ActionResult的奇怪表单行为
这似乎不应该起作用,所以我不知道为什么会起作用。我喜欢这个结果,但担心我不能依赖它,因为我不知道它是如何工作的Asp.net mvc 具有Submit ActionResult的奇怪表单行为,asp.net-mvc,Asp.net Mvc,这似乎不应该起作用,所以我不知道为什么会起作用。我喜欢这个结果,但担心我不能依赖它,因为我不知道它是如何工作的 [HttpGet] public ActionResult Modify(System.Guid id) { return View("Modify", LoadFromDatabase(id)); } [HttpPost] public ActionResult Modify(CaseModel myModel) { //So odd behavior. If I r
[HttpGet]
public ActionResult Modify(System.Guid id)
{
return View("Modify", LoadFromDatabase(id));
}
[HttpPost]
public ActionResult Modify(CaseModel myModel)
{
//So odd behavior. If I redirect to the actual GET Modify,
//I loose any changes on the form. however if I perform the
//same actions but here in the Post... all my unsaved changes
//stick..why?
//This one wipes any edits
return RedirectToAction("Modify", new { id = myModel.ID});
//This one actually leaves all my changes, even though
//I am re-creating the model from the database just like
//the other ActionResult
return View("Modify", LoadFromDatabase(myModel.ID));
}
如上所述
你可以放心
RedirectToAction()
重定向到一个新页面,该页面根据ID
属性初始化CaseModel
的新实例(即从存储库读取值。返回视图()
正在返回CaseModel
的当前实例,如果您使用HtmlHelper
方法生成表单控件,它将使用ModelState
中的值,这些值是在您提交表单时由DefaultModelBinder
添加的。有关该行为的更多解释,请参阅)@Html.TextBoxFor(m=>m.someProperty)
将显示您发布的值,而@Model.someProperty
将显示您在post方法中设置的“更新”值
工作完美。HTML帮助程序用ModelState(又称ViewState)覆盖我的模型值。您可以依赖它
RedirectToAction()
重定向到一个新页面,该页面根据ID
属性初始化CaseModel
的新实例(即从存储库读取值。返回视图()
正在返回CaseModel
的当前实例,如果您使用HtmlHelper
方法生成表单控件,它将使用ModelState
中的值,这些值是在您提交表单时由DefaultModelBinder
添加的@Html.TextBoxFor(m=>m.someProperty)
将显示您发布的值,而@Model.someProperty
将显示您在post methodPerfect!中设置的“更新”值!。。。我不知道HTML助手会用ModelState(又称ViewState?)覆盖我的模型值。。。。这就解释了。。。谢谢