【问题标题】:MVC POST Won't Bind Model...Result is nullMVC POST 不会绑定模型...结果为空
【发布时间】:2017-02-02 16:58:58
【问题描述】:

我已经阅读了许多与此相关的报告问题,但没有一个能够解决我遇到的问题。

型号:

public class MySoftwareResults
{
    public Shopping_MachineInformation MachineInformation { get; set; }
    public ShoppingUserInformation UserInformation { get; set; }
    public List<Shopping_MySoftwareResults> ApplicationsList { get; set; }
    public string Requester { get; set; }

    public MySoftwareResults()
    {
        MachineInformation = new Shopping_MachineInformation();
        UserInformation = new ShoppingUserInformation();
        ApplicationsList = new List<Shopping_MySoftwareResults>();
        Requester = "";
    }
}

表格:

@using (@Html.BeginForm("MySoftwareResults", "Client", FormMethod.Post))
    {
        <div class="form-group">
            <table class="table table-responsive list-view">
                <thead>
                <tr>
                    <th>Software</th>
                    <th>Cost</th>
                    <th>Requires Approval</th>
                    <th>Status</th>
                    <th>Select</th>
                </tr>
                </thead>
                <tbody>
                @foreach (var item in Model.ApplicationsList)
                {
                    <tr>
                        <td>
                            @Html.LabelForModel(item.Software)
                        </td>
                        <td>@Html.LabelForModel(item.Cost)</td>
                        <td>
                            @Html.LabelForModel(item.RequiresApproval)
                        </td>
                        <td>@Html.LabelForModel(item.Status)</td>
                        <td>
                            <input type="checkbox" id="Selected" name="Selected" value="@item.CollectionID"/>
                        </td>
                    </tr>
                }
                </tbody>
            </table>
        </div>
        <div class="form-group">
            <input type="submit" title="SUBMIT" class="btn btn-primary pull-right" id="butSubmit" />
        </div>
    }

表单完美填充。当我点击提交时,模型为空:

[HttpPost]
public ActionResult MySoftwareResults(MySoftwareResults results)
{            
    var selected = axp.euc.sdsassistance.core.Queries.Shopping_ParseCheckedItems(Request.Form["Selected"]);...
}

我尝试使用 Fiddler,但找不到任何东西来反映表单加载时正在传递的模型数据。

我被难住了。

【问题讨论】:

  • 很确定您需要先为您的值提供“名称”属性,然后才能通过 Request.Form[] 使用它们。不确定这是否适用于内置 Html.LabelForModel。
  • 别以为就是这样。最初我正在解决复选框的问题。我已经弄清楚了,并且 Request.Form[] 包含该数据。在该示例中,我构建以解决模型绑定到帖子的复选框问题。但在我的应用程序中不是。
  • 您生成的 html 与您的模型无关。您生成的唯一输入有name="Selected",并且只会绑定到public ActionResult MySoftwareResults(bool[] selected),这将是无用的。请参阅 this answer 以了解如何绑定到集合。并且表单只发回成功控件的名称/值对(&lt;input&gt;&lt;textarea&gt;&lt;select&gt;
  • 啊!呸!我更接近让这个绑定。我已经设法让 ApplicationList 绑定。我如何让其他子模型绑定?我不想显示数据,只想将其传输到控制器?我试过@Html.HiddenFor(x=>x.MachineInformation),但没用。

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


【解决方案1】:

感谢 Stephen Muecke 指导我更好地了解建模在表单中的工作原理。通过执行以下操作,我能够将我的数据绑定到 POST 调用。

对于我没有显示数据但需要将它们绑定到我所做的 POST 的模型:

<div style="display: none">
            @Html.EditorFor(x=>x.MachineInformation)
            @Html.EditorFor(x=>x.UserInformation)
        </div>

对于我想要显示数据的模型:

@for (int i = 0; i < Model.ApplicationsList.Count; i++)
                {
                    <tr>
                        <td>
                            @Html.LabelFor(m => m.ApplicationsList[i].Software, new {@class = "form-controller"})
                        </td>
                        <td>@Html.LabelFor(m => m.ApplicationsList[i].Cost, new {@class = "form-controller"})</td>
                        <td>
                            @Html.LabelFor(m => m.ApplicationsList[i].RequiresApproval, new {@class = "form-controller"})
                        </td>
                        <td>@Html.LabelFor(m => m.ApplicationsList[i].Status, new {@class = "form-controller"})</td>
                        <td>
                            <input type="checkbox" id="Selected" name="Selected" value="@Model.ApplicationsList[i].CollectionID"/>
                        </td>
                    </tr>
                }

当我点击提交我在控制器中填充的模型时!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多