【问题标题】:ASP.NET MVC5 form values not binding to modelASP.NET MVC5 表单值未绑定到模型
【发布时间】:2017-02-24 20:38:58
【问题描述】:

我有一个无法将值绑定到我的模型的表单。模型在发布操作时为空。

我已将所有内容简化为最简单的示例,但仍然无法绑定。

型号

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" />
}

控制器动作

[HttpPost]
public ActionResult Index(simplemodel model)
{
    string ReceivedVal = model.SomeVal;

    //do whatever...
}

这很简单,但我的模型没有绑定。

ReceivedVal 始终为空。我有很多其他地方可以毫无问题地绑定到模型。我很困惑。我可能会错过什么?

如果我将控制器操作更改为接收 FormCollection,我确实会收到“SomeVal”。所以我知道它已正确发布,只是不会绑定到模型。

【问题讨论】:

  • 请注意,model-view-controller 标签是针对有关模式的问题。 ASP.NET-MVC 实现有一个特定的标记。
  • 愿你能提供你的原始html

标签: asp.net-mvc binding model


【解决方案1】:

我几乎放弃了搜索......并且只是阅读了一些没有选择类似问题的答案,并了解到我的模型必须具有属性,而不仅仅是公共字段。我一定是偷懒了,没有添加 { get;放; }

我从来没有意识到这是必要的!

对不起!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多