我有一个表单,无法将值绑定(bind)到我的模型。模型在后期操作中为空。
我已将所有内容简化为最简单的示例,但仍然无法将其绑定(bind)。
模型
public class simplemodel
{
public string SomeVal;
}
景色
@model MyApp.Models.simplemodel
@using(Html.BeginForm("Index", "Test", FormMethod.Post))
{
@Html.TextBoxFor(m => m.SomeVal);
<input type="submit" value="submit" id="btnSubmit" />
}
Controller 操作
[HttpPost]
public ActionResult Index(simplemodel model)
{
string ReceivedVal = model.SomeVal;
//do whatever...
}
这很简单,但我的模型没有绑定(bind)。
ReceivedVal 始终为空。我还有很多其他地方可以毫无问题地绑定(bind)模型。我很困惑。我可能会错过什么?
如果我更改 Controller 操作以接收 FormCollection,我确实会收到“SomeVal”。所以我知道它已正确发布,只是不会绑定(bind)到模型。
最佳答案
我几乎放弃了搜索......而只是阅读了一些未针对类似问题选择的答案,并了解到我的模型必须具有属性,而不仅仅是公共(public)字段。我一定是太懒了,没有添加 { get;放; }
我从来没有意识到这是必要的!
抱歉!
关于asp.net-mvc - ASP.NET MVC5 表单值未绑定(bind)到模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42447868/