简体   繁体   English

MVC POST无法绑定模型…结果为null

[英]MVC POST Won't Bind Model…Result is null

I've read through many reported issues relating to this, none have resolved the problem I'm having. 我已阅读了许多与此相关的报告问题,但都没有解决我遇到的问题。

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 = "";
    }
}

Form: 形成:

@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>
    }

The form populates perfectly. 表单完美填充。 When I click on Submit the Model is empty: 当我单击“提交”时,模型为空:

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

I tried using Fiddler, but I can't find anything to reflect the model data being passes when the form loads. 我尝试使用Fiddler,但找不到任何形式来反映在加载表单时传递的模型数据。

I'm stumped. 我很沮丧

Thanks for Stephen Muecke for directing me to better understanding how the modeling works in forms. 感谢Stephen Muecke指导我更好地理解建模在表单中的工作方式。 I was able to bind my data over to the POST call by doing the following. 通过执行以下操作,我可以将数据绑定到POST调用。

For the models I wasn't displaying data for, but needed them to be bind to the POST I did: 对于这些模型,我没有显示数据,但需要将它们绑定到我所做的POST:

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

For the Model I wanted to display data for I did: 对于模型,我想显示我所做的数据:

@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>
                }

When I clicked on Submit my Model populated in the controller!! 当我单击Submit时,我的模型将填充到控制器中!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM