【问题标题】:Why is my model not passed as parameter with form post为什么我的模型没有作为参数传递给表单帖子
【发布时间】:2013-04-30 12:48:53
【问题描述】:

我无法理解为什么在发布表单时我的模型没有连同它的值一起传递给我的控制器。

我有一个从 Web 服务获取的强类型模型 (UnitContract) 的视图,该模型包含一组值。在我的操作中,我试图获取模型中存在的 int ID 和 bool Disabled 字段。调试时,我看到从表单传递的模型根本不包含任何值。我错过了什么?

我的观点(UnitContract 作为强类型模型):

...
<form class="pull-right" action="~/UnitDetails/EnableDisableUnit" method="POST">
        <input type="submit" class="k-button" value="Enable Unit"/>
    </form>

我的控制器操作:

[HttpPost]
public ActionResult EnableDisableUnit(UnitContract model)
{
    var client = new UnitServiceClient();
    if (model.Disabled)
    {
        client.EnableUnit(model.Id);
    }
    else
    {
        client.DisableUnit(model.Id);
    }

    return RedirectToAction("Index", model.Id);
}

【问题讨论】:

  • 您的表单看起来很空...
  • 修改您的问题以包括您视图中(应该)映射到UnitContract的字段。
  • 这就是您表单中的所有代码吗?似乎应该有更多......
  • 您的表单是否绑定到 UnitContract 模型?如果是这样,你是如何通过它的?您是否使用您正在寻找的值填充模型 (model.Id) ?更多细节...
  • 您必须在表单中呈现输入,这些输入将绑定到模型类的属性。

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

听起来您需要将模型中的字段添加到表单中。假设您的视图接受 UnitContract 模型,那么这样的事情应该可以工作:

<form class="pull-right" action="~/UnitDetails/EnableDisableUnit" method="POST">
    @Html.HiddenFor(x => x.Id)
    @Html.HiddenFor(x => x.Disabled)
    <input type="submit" class="k-button" value="Enable Unit"/>
</form>

现在,当您提交表单时,它应该将字段提交给您的模型。

【讨论】:

    【解决方案2】:

    MVC 框架将使用表单中的数据来创建模型。由于您的表单本质上是空的,因此没有可用于创建模型的数据,因此您获得了一个没有填充任何数据的对象。

    当您发布表单时,在请求中从浏览器发送的唯一数据是表单内的数据。您必须将模型中属性的数据作为字段放入表单中,以便填充模型。

    【讨论】:

      【解决方案3】:

      考虑使用@Html.HiddenFor()。将这些放入您的表单中,您希望看到的数据会发送回您的控制器。例如,您的表单看起来像...

      <form class="pull-right" action="~/UnitDetails/EnableDisableUnit" method="POST">
          @Html.HiddenFor(x => x.Id)
          @Html.HiddenFor(x => x.IsDisabled)
          <input type="submit" class="k-button" value="Enable Unit"/>
      </form>
      

      【讨论】:

        【解决方案4】:

        假设你有一个这样的模型:

        public class UnitContract
        {
            public int Id { get; set; }
            public DateTime SignedOn { get; set; }
            public string UnitName { get; set; }
        }
        

        您的视图将如下所示:

        @using (Html.BeginForm()) {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true)
        
            <fieldset>
                <legend>UnitContract</legend>
        
                @Html.HiddenFor(model => model.Id)
        
                <div class="editor-label">
                    @Html.LabelFor(model => model.SignedOn)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.SignedOn)
                    @Html.ValidationMessageFor(model => model.SignedOn)
                </div>
        
                <div class="editor-label">
                    @Html.LabelFor(model => model.UnitName)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.UnitName)
                    @Html.ValidationMessageFor(model => model.UnitName)
                </div>
        
                <p>
                    <input type="submit" value="Save" />
                </p>
            </fieldset>
        }
        

        在您的控制器中:

            [ValidateAntiForgeryToken]
            [HttpPost]
            public ActionResult Edit(UnitContract unitContract)
            {
                // do your business here .... unitContract.Id has a value at this point
                return View();
            }
        

        希望这有帮助。

        【讨论】:

          猜你喜欢
          • 2020-01-16
          • 1970-01-01
          • 2023-03-18
          • 2017-08-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多