Asp.net mvc 发布后填充文本框时出现MVC 2问题
我在MVC2应用程序中有以下操作:Asp.net mvc 发布后填充文本框时出现MVC 2问题,asp.net-mvc,asp.net-mvc-2,Asp.net Mvc,Asp.net Mvc 2,我在MVC2应用程序中有以下操作: public ActionResult Index() { return View(new TestModel() { MyValue = "ValueBeforePost" }); } [AcceptVerbs(HttpVerbs.Post)] public ActionResult Index(TestModel m) { m.MyValue = "ValueSetInPos
public ActionResult Index()
{
return View(new TestModel() { MyValue = "ValueBeforePost" });
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(TestModel m)
{
m.MyValue = "ValueSetInPost";
return View(m);
}
在我看来,我有
<% using (MvcForm f = Html.BeginForm("Index", "Home")) %>
<% { %>
<%= Html.TextBoxFor(m => Model.MyValue) %>
<input type="submit" value="Submit" />
<% } %>
Model.MyValue)%%>
发布后,我的文本框中的值没有填充我在操作中设置的新值(m.MyValue=“ValueSetInPost”),它似乎是根据Request.Form(在本例中是Request.Form[“MyValue”,即“ValueBeforePost”)中保存的值填充的
如何更改帖子中的值?您需要操作
ModelState
才能使其正常工作。有一个包含更多详细信息的示例
textbox帮助程序将使用modelstate中的AttemptedValue重新填充文本框,并将值发布到控制器。最终效果是modelstate中的数据将覆盖您在发布操作中输入模型并传递给视图的任何内容。您需要从的
modelstate
中删除该值您打算对其进行修改,否则Html帮助程序将使用原始发布值:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(TestModel m)
{
ModelState.Remove("MyValue");
m.MyValue = "ValueSetInPost";
return View(m);
}
你能确认当你把断点放在帖子里时,它正在命中它吗?是的,它正在命中断点