【发布时间】:2011-11-24 13:36:12
【问题描述】:
在我的项目中,DropDownListFor(x => x 代码位于 EditorTemplate 中。它用于填充数据表,该表的字段之一是下拉列表。虽然一切都在渲染没有问题,但下拉列表不默认为我在 ViewModel 中设置的预选项目。我没有看到什么?
代码如下:
视图模型:
public class FooDetailViewModel : ViewModelBase
{
public List<FooPermissionObject> FooPermissions { get; set; }
}
强类型模型对象:
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>
编辑器模板:
@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>
有人知道为什么这不会用由 Reasons 集合中的 Reason 值表示的 Object 填充 DropDownListFor 吗?
【问题讨论】:
标签: c# .net asp.net-mvc-3 razor