Mar*_*iah 5 asp.net-mvc asp.net-mvc-3
我在这里错过了什么?
视图模型:
public class ViewModel
{
public IDictionary<int, string> Entities { get; set; }
public int EntityId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器:
public override ActionResult Create(string id)
{
ViewModel = new ViewModel();
IEnumerable<Entity> theEntities = (IEnumerable < Entity >)db.GetEntities();
model.Entities= theEntities.ToDictionary(x => x.Id, x => x.Name);
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
视图:
<div class="editor-field">@Html.DropDownListFor(model => model.EntityId,
new SelectList(Model.Entities, "Id", "Name"))</div>
</div>
Run Code Online (Sandbox Code Playgroud)
错误:
DataBinding:'System.Collections.Generic.KeyValuePair ....不包含名为'Id'的属性
Kei*_*ith 14
KeyValuePair有财产Key和Value.您最好声明Entities为类型IEnumerable<Entity>,然后您的视图将按原样运行:
public class ViewModel
{
public IEnumerable<Entity> Entities { get; set; }
public int EntityId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您确实需要使用Dictionary <>,请更改您的视图:
<div class="editor-field">
@Html.DropDownListFor(model => model.EntityId, new SelectList(Model.Entities, "Key", "Value"))
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11413 次 |
| 最近记录: |