我知道这个问题已经被问了大约100次,我已经完成了它们,我无法看到任何我可能做错的事情。任何帮助将不胜感激
回发时,ImportModel列表为空。
@model Models.ImportModel
<div class="main-content">
<div class="container">
<h1>Uploaded Cars</h1>
@using (Html.BeginForm("Confirm", "Import", FormMethod.Post, new { @class = "form-horizontal" }))
{
<table id="importVehiclesTable" class="table">
<thead>
<tr>
<th>Registration</th>
<th>Details</th>
<th></th>
</tr>
</thead>
<tbody>
@for (int i = 0; i < Model.Vehicles.Count; i++)
{
<tr>
<td>@Html.DisplayFor(m => m.Vehicles[i].RegistrationNumber)</td>
<td>@Html.DisplayFor(m => m.Vehicles[i].Name)</td>
<td></td>
</tr>
}
</tbody>
</table>
<br />
<br />
<input type="submit" value="Import Vehicles" class="btn btn-primary" />
<input type="reset" value="Back" class="btn" onclick="history.back();" />
}
</div>
我的模特,
/// <summary>
///
/// </summary>
public class ImportModel : BaseModel
{
/// <summary>
/// Gets or sets the cars.
/// </summary>
/// <value>
/// The cars.
/// </value>
public List<VehicleAddViewModel> Vehicles { get; set; }
}
我的行动,但车辆obj为空。
[HttpPost]
public ActionResult Confirm(ImportModel importModel)
{
//Will be saving to the DB here
return null;
}
答案 0 :(得分:2)
您正在使用DisplayFor()
MVC不会发布回来。
尝试将它们作为表单元素
HiddenFor()
或TextBoxFor()
相关答案:
DisplayFor is not keeping data in ViewModel on PostBack
Html.DisplayFor not posting values to controller in ASP.NET MVC 3