我可能在某个地方犯了一个愚蠢的错误.我将非常感谢您的帮助.
我有一个带有单个可编辑字段的MVC3应用程序示例,该字段使用TextBoxFor方法显示给用户.在索引(POST)操作中,我更改了值,但它仍然保持不变.我究竟做错了什么?
我的代码:
型号:
public class TestModel
{
public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
视图:
using (Html.BeginForm())
{
@Html.TextBoxFor(m => m.Name)
<input type="submit" />
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public ActionResult Index()
{
return View("Index", new TestModel() { Name = "Before post" });
}
[HttpPost]
public ActionResult Index(TestModel model)
{
model.Name = "After post";
return View("Index", model);
}
Run Code Online (Sandbox Code Playgroud)
如果我用TextBox或DisplayTextFor替换TextBoxFor,那么它可以正常工作.
小智 13
我相信在设置新值之前,您必须ModelState.Clear()在[HttpPost]操作中调用.
根据这个答案,它有一个很好的解释:如何更新文本框值@ Html.TextBoxFor(m => m.MvcGridModel.Rows [j] .Id)
也看到了这一点:ASP.NET MVC 3 Ajax.BeginForm和Html.TextBoxFor没有反映在服务器上完成的更改
尽管您似乎没有使用Ajax.BeginForm,但行为是相同的.
包括@Scheien建议的一个例子:
[HttpPost]
public ActionResult Index(TestModel model)
{
ModelState.Clear();
model.Name = "After post";
return View("Index", model);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2556 次 |
| 最近记录: |