1

私はこれを少し調べましたが、同様の状況またはMVC3を完全に扱う答えは見つかりませんでした。私が使用しているViewModelには、別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>
4

2 に答える 2

2

私はそれ@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
}

コントローラにデータをロードします。

[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);
}

そして、ビューでそれをバインドします:

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

これがお役に立てば幸いです。説明が必要な場合はお知らせください。

于 2013-03-20T03:18:07.993 に答える
1

ViewBag私が使用していた要素がリストアイテムのプロパティの1つと同じ名前であったため、このエラーがスローされました。

解決策はに変更ViewBag.StateCodeすることでしたViewBag.StateCodeList

于 2013-03-20T16:05:00.983 に答える