在我的項目中,DropDownListFor(x => x代碼位於EditorTemplate中,它用於填充數據表,其中一個表的字段是下拉列表,儘管一切都沒有問題呈現,但drop下拉列表不默認爲預先選定的項目我的視圖模型是設置什麼是我沒有看到DropDownListFor()沒有填充ViewModel中的預選項目?
代碼如下:?
視圖模型:
public class FooDetailViewModel : ViewModelBase
{
public List<FooPermissionObject> FooPermissions { get; set; }
}
強類型模型object:
public class FooPermissionObject
{
public string Name { get; set; }
public int Reason { get; set; }
public IEnumerable<SelectListItem> Reasons { get; set; }
public bool Selected { get; set; }
}
控制器:
var viewModel = new StockLineManagementDetailViewModel();
using (_model)
{
foreach (var company in _model.GetAllRecords<Company>())
{
var permissionModel = new FooPermissionObject
{
Name = company.Name,
Selected = true,
Reasons = _model.GetAllRecords<FooPermissionReason>()
.ToList()
.Select(x => new SelectListItem
{
Value = x.FooPermissionReasonId.ToString(),
Text = x.FooPermissionReasonDesc
}),
Reason = record.FooPermissionReasonId
};
viewModel.FooPermissions.Add(permissionModel);
}
}
的觀點:
<table id="myTable" class="tablesorter" style="width:98%">
<thead>
<tr>
<th>
Name
</th>
<th>
Excluded
</th>
<th>
Reason for Exclusion
</th>
</tr>
</thead>
<tbody>
@Html.EditorFor(x => x.FooPermissions)
</tbody>
</table>
的EditorTemplate:
@model FooPermissionObject
<tr>
<td>
@Html.DisplayFor(x => x.Name, new { @readonly = "readonly"})
@Html.HiddenFor(x => x.Name)
</td>
<td>
@Html.CheckBoxFor(x => x.Selected)
</td>
<td>
@Html.DropDownListFor(x => x.Reason, Model.Reasons)
</td>
</tr>
任何人有任何想法,爲什麼這會不會與代表的對象填充DropDownListFor來自Reasons集合中的Reason價值?
據我所知(這可能是錯誤的)設置Html幫手爲Html.DropDownListFor(x => x.Reason,Model.Reasons )自動綁定選定的項目作爲原因字段? – M05Pr1mty
是的,用戶選擇的值將是在模型中爲原因屬性設置的值,但與預選項目無關。 –
噢,真的嗎?所以即使遵循dasheddot的建議也不會起作用,因爲我仍然只設置用戶選擇內容時綁定的字段? – M05Pr1mty