Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP MVC3错误-没有类型为';IEnumerable<;选择列表项>';这是关键_Asp.net Mvc_Asp.net Mvc 3_List_Viewmodel_Selectlist - Fatal编程技术网

Asp.net mvc ASP MVC3错误-没有类型为';IEnumerable<;选择列表项>';这是关键

Asp.net mvc ASP MVC3错误-没有类型为';IEnumerable<;选择列表项>';这是关键,asp.net-mvc,asp.net-mvc-3,list,viewmodel,selectlist,Asp.net Mvc,Asp.net Mvc 3,List,Viewmodel,Selectlist,我对此进行了一些研究,但还没有找到一个解决类似情况或MVC3的答案。在我使用的ViewModel中,我有一个单独模型的列表(List,它是AgentId模型的列表) 在该控制器的创建页面中,我需要一个输入部分,用于将5项添加到此列表中。但是,在页面加载之前,我收到以下错误消息: 没有“IEnumerable”类型的ViewData项的键为“BankListAgentId[0]。状态代码。 以下是我正在使用的ViewModel: public class BankListViewModel {

我对此进行了一些研究,但还没有找到一个解决类似情况或MVC3的答案。在我使用的ViewModel中,我有一个单独模型的列表(
List
,它是
AgentId
模型的列表)

在该控制器的
创建
页面中,我需要一个输入部分,用于将5项添加到此列表中。但是,在页面加载之前,我收到以下错误消息:

没有“IEnumerable”类型的ViewData项的键为“BankListAgentId[0]。状态代码。

以下是我正在使用的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; }
}
公共类BankListViewModel
{
公共int ID{get;set;}
公共字符串ContentTypeID1{get;set;}
通过{get;set;}创建的公共字符串
公共字符串MANonresBizNY{get;set;}
公共字符串LastChangeOperator{get;set;}
公共可为空的LastChangeDate{get;set;}
公共列表BankListAgentId{get;set;}
公共列表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>

统计数据已修复
州代码
代理ID
@Html.DropDownListFor(model=>model.BankListAgentId[0]。状态码,
(选择列表)ViewBag.StateCode(“”)
@Html.EditorFor(model=>model.BankListAgentId[0].AgentId)
@Html.ValidationMessageFor(model=>model.BankListAgentId[0].AgentId)
@Html.DropDownListFor(model=>model.BankListAgentId[1]。状态码,
(选择列表)ViewBag.StateCode(“”)
@Html.EditorFor(model=>model.BankListAgentId[1].AgentId)
@Html.ValidationMessageFor(model=>model.BankListAgentId[1].AgentId)
+

我相信
@Html.DropDownListFor()
需要一个
IEnumerable
,您可以通过以下方式绑定它:

在ViewModel中:

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

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

    // ... other properties here
}

我希望这有帮助。如果需要澄清,请告诉我。

由于我使用的
ViewBag
元素与列表项属性之一同名,因此引发此错误


解决方案是将
ViewBag.StateCode
更改为
ViewBag.StateCodeList

,因为缺少错误声明“ViewBag.StateCode”。您是否在返回view的操作中定义了“ViewBag.StateCode”,我正在尝试使用列表进行输入,因此在呈现视图时它应该为空。也许我需要在控制器中初始化它?
[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)