假设我有一个模型:
public MyClass
{
public int Id {get;set;}
public string Name {get;set;}
public int ContentId {get;set;}
}
假设我访问了 Controller 的操作:
[HttpGet]
public ActionResult Create(int id)
{
MyClass mc = new MyClass();
mc.Id = 49;
mc.ContentId = id;
mc.Name = "Sample";
return View("Create", mc);
}
“创建” View 是 MyClass 的强类型 View ,它有助手 @Html.TextBoxFor(x => x.Id)。
如果我通过调用 MyController/Create?id=15 调用操作,文本框将显示值 15 而不是 49。MVC 将忽略我在操作中设置的 ID 属性,并使用查询中的属性。
考虑到这种行为在任何地方都没有记录,我觉得这很奇怪。
对此有什么好的意见吗?
最佳答案
在 Action 开始时使用 ModelState.Clear()
关于asp.net-mvc - MVC4 TextBoxFor 覆盖模型属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12716297/