【发布时间】:2013-11-08 15:21:11
【问题描述】:
我相信我在这里遗漏了一些非常微不足道的东西,但我没有发现它。我有一个 Post 方法,它修改了 Model 属性。然后我希望该模型属性反映新的属性值。所以这里是碎片:
控制器:
[HttpPost]
public ActionResult Index(HomeModel model)
{
ModelState.Clear(); //Didn't help
model.MyValue = "Hello this is a different value";
return View(model);
}
型号:
public class HomeModel
{
[Display(Name = "My Message")]
public string MyValue { get; set; }
}
查看:
@model MyApp.Models.HomeModel
@{
ViewBag.Title = "My MVC App";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div>
@using (Html.BeginForm("Index", "Home"))
{
<h5>Hello</h5>
<input id="SendMessage" type="submit" value="Send Message"/>
@Html.LabelFor(m => m.MyValue)
}
</div>
</body>
</html>
当我调试控制器时,我可以看到更新的模型,但我的 LabelFor 始终具有 Display 属性,而不是我提供的 "Hello this is a different value" 的值。我在这里缺少什么,这个标签没有更新?
【问题讨论】:
标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor