MVC POST不会绑定模型...结果为空

时间:2017-02-02 16:58:59

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

我已经阅读了许多与此相关的报道问题,没有人解决我遇到的问题。

型号:

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,但是我无法找到任何内容来反映表单加载时传递的模型数据。

我很难过。

1 个答案:

答案 0 :(得分:0)

感谢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>
                }

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