簡體   English   中英

ASP MVC3錯誤 - 沒有類型為&#39;IEnumerable的ViewData項 <SelectListItem> &#39;那是關鍵

[英]ASP MVC3 Error - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key

我已經研究了一下,並沒有找到一個完全處理類似情況或MVC3的答案。 在我正在使用的ViewModel中,我有一個單獨的模型List<AgentId>List<AgentId> ,它是AgentId模型的列表)。

在此控制器的“ Create頁面中,我需要一個輸入部分,用於將5個項目添加到此列表中。 但是,在頁面加載之前,我收到此錯誤消息:

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'BankListAgentId[0].StateCode'.

這是我正在使用的ViewModel:

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>

我相信@Html.DropDownListFor()期待一個IEnumerable<SelectListItem> ,你可以通過以下方式綁定它:

在您的ViewModel中:

public class BankListViewModel
{
    public string StateCode { get; set; }

    [Display(Name = "State Code")]
    public IEnumerable<SelectListItem> BankListStateCode { get; set; }

    // ... other properties here
}

在您的Controller中加載數據:

[HttpGet]
public ActionResult Create()
{
    var model = new BankListViewModel()
    {
        // load the values from a datasource of your choice, this one here is manual ...
        BankListStateCode = new List<SelectListItem>
        {
            new SelectListItem
            {
                Selected = false,
                Text ="Oh well...",
                Value="1"
            }
        }
    };

    return View("Create", model);
}

然后在View中綁定它:

 @Html.LabelFor(model => model.BankListStateCode)
 @Html.DropDownListFor(model => model.StateCode, Model.BankListStateCode)

我希望這有幫助。 如果您需要澄清,請告訴我。

由於我使用的ViewBag元素與列表項屬性之一具有相同的名稱,因此拋出此錯誤。

解決方案是將ViewBag.StateCode更改為ViewBag.StateCodeList

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM