我已经研究了这一点,但没有找到相当类似的情况或MVC3的答案。在我使用的ViewModel中,我有一个单独模型的列表(List<AgentId>
,它是AgentId
模型的列表)。ASP MVC3错误 - 没有类型为'IEnumerable <SelectListItem>'的ViewData项目,其中包含密钥
在这个控制器的Create
页面中,我需要一个输入部分来添加5个项目到这个列表。然而,之前的页面,甚至加载,我收到此错误信息:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'BankListAgentId[0].StateCode'.
这里是视图模型我使用:
public class BankListViewModel
{
public int ID { get; set; }
public string ContentTypeID1 { get; set; }
public string CreatedBy { get; set; }
public string MANonresBizNY { get; set; }
public string LastChangeOperator { get; set; }
public Nullable<System.DateTime> LastChangeDate { get; set; }
public List<BankListAgentId> BankListAgentId { get; set; }
public List<BankListStateCode> BankListStateCode { get; set; }
}
,这里是这是使用的问题的看法的部分:
<fieldset>
<legend>Stat(s) Fixed</legend>
<table>
<th>State Code</th>
<th>Agent ID</th>
<th></th>
<tr>
<td>
@Html.DropDownListFor(model => model.BankListAgentId[0].StateCode,
(SelectList)ViewBag.StateCode, " ")
</td>
<td>
@Html.EditorFor(model => model.BankListAgentId[0].AgentId)
@Html.ValidationMessageFor(model => model.BankListAgentId[0].AgentId)
</td>
</tr>
<tr>
<td>
@Html.DropDownListFor(model => model.BankListAgentId[1].StateCode,
(SelectList)ViewBag.StateCode, " ")
</td>
<td>
@Html.EditorFor(model => model.BankListAgentId[1].AgentId)
@Html.ValidationMessageFor(model => model.BankListAgentId[1].AgentId)
</td>
<td id="plus2" class="more" onclick="MoreCompanies('3');">+</td>
</tr>
</table>
</fieldset>
根据错误语句“ViewBag.StateCode”丢失。你有没有定义“ViewBag.StateCode”在操作返回视图 – Satpal 2013-03-20 06:21:41