我已經研究了這一點,但沒有找到相當類似的情況或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