1
我有接受一個複雜的對象參數,它本身所具有的複雜類型集合屬性的控制器。表單發佈後,模型綁定不起作用於我的損壞屬性(它始終爲空)。如何建立下拉菜單爲我的損失屬性,因此該模型將與所選值填充當HTML表單發送到控制器?ASP.NET MVC的SelectList項目與模型綁定
注意,我是新來的ASP.NET MVC,這是預先寫好的代碼,我一直在問調試。
查看模型1:
public class EditInspectionViewModel
{
public long? InsRefreshSeq { get; set; }
public string Inspector { get; set; }
public string VIN { get; set; }
public string Manufacturer { get; set; }
public string Plant { get; set; }
public string Model { get; set; }
public IEnumerable<DamageViewModel> Damages { get; set; }
public IEnumerable<GETAREAResult> AreaFilterOptions { get; set; }
public IEnumerable<GETDMGTYPEResult> DamageTypeFilterOptions { get; set; }
public IEnumerable<GETSEVERITYResult> SeverityFilterOptions { get; set; }
public IEnumerable<GETGRIDResult> GridFilterOptions { get; set; }
}
查看模型2:
public class DamageViewModel
{
public long DamageId { get; set; }
public string Area { get; set; }
public string AreaCode { get; set; }
public string TypeCode { get; set; }
public string SeverityCode { get; set; }
public string GridCode { get; set; }
public string Type { get; set; }
public string Severity { get; set; }
public string Grid { get; set; }
public bool HasError { get; set; }
public bool HasAreaError { get; set; }
public bool HasDamageTypeError { get; set; }
public bool HasSeverityError { get; set; }
public bool HasImages { get; set; }
}
控制器編輯方法:
[HttpGet]
public virtual ActionResult Edit(long id)
{
EditInspectionViewModel viewModel = _getInspectionForEditingQuery.Execute(CurrentUserId, id);
viewModel.InsRefreshSeq = 0;
viewModel.AreaFilterOptions = _refService.GetAreas().OrderBy(x => x.ARECODE);
viewModel.DamageTypeFilterOptions = _refService.GetDamageTypes().OrderBy(x => x.DTPCODE);
viewModel.SeverityFilterOptions = _refService.GetSeverities().OrderBy(x => x.SEVCODE);
viewModel.GridFilterOptions = _refService.GetQuadrants().OrderBy(x => x.QUDCODE);
return View(viewModel);
}
[HttpPost]
public virtual ActionResult Edit(long id, EditInspectionViewModel editedInspection)
{
if (ModelState.IsValid)
{
_updateErrorInspectionCommand.Execute(editedInspection);
return View(editedInspection);
}
return View(editedInspection);
}
這裏是一個顯示檢查損害的局部剃刀視圖(二者此視圖和外視圖被鍵入到EditInspectionViewModel):
@model AIM.NewCars.Model.Inspections.EditInspectionViewModel
@{
}
<div class="panel panel-info">
<div class="panel-heading clearfix">
</div>
<table class="table table-condensed">
<thead>
<tr>
<th>Area</th>
<th>Type</th>
<th>Severity</th>
<th>Grid</th>
<th>Images</th>
<th>Remove</th>
</tr>
</thead>
<tbody>
@foreach (var damage in Model.Damages)
{
if (damage.HasError)
{
IEnumerable<SelectListItem> areas = Model.AreaFilterOptions.Select(x => new SelectListItem() { Value = x.ARECODE, Text = x.ARECODE + "-" + x.AREDESC, Selected = damage.AreaCode == x.ARECODE });
IEnumerable<SelectListItem> damageTypes = Model.DamageTypeFilterOptions.Select(x => new SelectListItem() { Value = x.DTPCODE, Text = x.DTPCODE + "-" + x.DTPDESC, Selected = damage.TypeCode == x.DTPCODE });
IEnumerable<SelectListItem> severities = Model.SeverityFilterOptions.Select(x => new SelectListItem() { Value = x.SEVCODE, Text = x.SEVCODE + "-" + x.SEVDESC, Selected = damage.SeverityCode == x.SEVCODE });
IEnumerable<SelectListItem> grids = Model.GridFilterOptions.Select(x => new SelectListItem() { Value = x.QUDCODE, Text = x.QUDCODE + "-" + x.QUDDESC, Selected = damage.GridCode == x.QUDCODE });
<tr class="damage-container">
<td>
<input type="hidden" name="@("InspectionDamages[" + Model.Index + "].Index")" value="@Model.Index" />
@Html.DropDownList("InspectionDamages[" + damage.DamageId + "].AreaCode", areas, string.Empty, new { @class = "form-control js-damage-field" })
</td>
<td>
@Html.DropDownList("Damages[" + damage.DamageId + "].DamageTypeCode", damageTypes, string.Empty, new { @class = "form-control js-damage-field" })
</td>
<td>
@Html.DropDownList("InspectionDamages[" + damage.DamageId + "].SeverityCode", severities, string.Empty, new { @class = "form-control js-damage-field" })
</td>
<td>
@Html.DropDownList("InspectionDamages[" + damage.DamageId + "].GridCode", grids, string.Empty, new { @class = "form-control js-damage-field" })
</td>
}
</tbody>
</table>
</div>
後的HTTP GET,下拉菜單會根據他們的價值觀就好獲取填充和選擇,但是當我發佈形式的賠償屬性爲null,當它到達後的操作方法。
使您的集合屬性'IList'並使用'for'循環(不是'foreach'循環或爲您的類型使用自定義的'EditorTemplate'。 –