asp.net-mvc - ASP.NET MVC5 表单值未绑定(bind)到模型

标签 asp.net-mvc binding model

我有一个表单,无法将值绑定(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/

相关文章:

asp.net-mvc - 如何在 View 中加载部分 View ?

binding - JAX WSclient 将 stub 转换到绑定(bind)提供者

ruby-on-rails - rails : Update model attribute without invoking callbacks

java - 查找产品购买间隔

c# - MVC4 将消息从一个 Controller 传递到另一个 Controller

jquery - ASP.NET MVC 5 垂直表头

c# - Azure Redis 无法连接到 Redis 服务器

.net - xaml 中的值绑定(bind)语法是什么?

c# - 我应该绑定(bind)到 WPF Prism 中的 SharedService 属性吗

python - 模型中对象的总和