我相信我在这里遗漏了一些非常微不足道的东西,但我没有发现它。我有一个Post
修改模型属性的方法。然后我希望该模型属性反映新的属性值。所以这里是碎片:
控制器:
[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"
。我在这里缺少什么,这个标签没有更新?