【发布时间】: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 以了解如何绑定到集合。并且表单只发回成功控件的名称/值对(<input>、<textarea>和<select>) -
啊!呸!我更接近让这个绑定。我已经设法让 ApplicationList 绑定。我如何让其他子模型绑定?我不想显示数据,只想将其传输到控制器?我试过@Html.HiddenFor(x=>x.MachineInformation),但没用。
标签: c# asp.net-mvc asp.net-mvc-4 model