2014-03-12 27 views
0

'在我的MVC项目中运行时,我在当时按下编辑选项,错误发生具有'distic_id'键的ViewData项的类型为'System.Int32',但必须为'IEnumerable <SelectListItem>''

具有关键 'distic_id' 的类型为 'System.Int32',但必须是类型 '的IEnumerable <SelectListItem>'

在我看来代码ViewData的项目

@Html.DropDownListFor(m => m.distic_id, Model.disticlist) 

模型

public class city 
{ 
    public List<SelectListItem> disticlist { get; set; } 
    public int city_id { get; set; } 
    [Required] 
    [Display(Name = "enter the District name")] 
    public string city_name { get; set; } 
    [Required] 
    [Display(Name = "select district ")] 
    public int distic_id { get; set; } 
} 
+0

那么,如果编译器正确,请检查您的模型。我想是的。 – nvoigt

+0

发布您的模型类型。 –

+0

一些重播我 –

回答

0

,如果你想获得城市或地区名单在下拉列表,请参阅下面的代码

1)删除您的代码 2)创建这样 3一个模型)如果此下拉在多个页面中使用CREATE一个控制器像CommanController 4)写在这个控制器 一个方法请参见下面的代码

首先需要建立模型这样

public class Industry 
{ 
    public string Id { get; set; } 
    public string industryName { get; set; } 
    public string regexindustry { get; set; } 
} 
public class IndustryModel 
{ 
    public SelectList industryList { get; set; } 
} 

在控制器 两个步骤1是在与使用目的

ViewBag.list=obj.getIndustryList(); 

public List<Industry> getIndustryList() 
    { 
     List<Industry> objindustry = new List<Industry>(); 
     var connString = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString); 
     SqlCommand sqlComm = new SqlCommand("sp_selIndustryMaster", connString); 
     connString.Open(); 
     sqlComm.CommandType = CommandType.StoredProcedure; 
     SqlDataReader sqldr = sqlComm.ExecuteReader(); 
     int count = 0; 
     while (sqldr.Read()) 
     { 
      if (count == 0) 
      { 
       objindustry.Add(new Industry { Id ="", industryName = "Select Industry" }); 
       count++; 
      } 
      else 
      { 
       objindustry.Add(new Industry { Id = Convert.ToString(sqldr["industryCode"]), industryName = sqldr["subindustry"].ToString() }); 
      } 
     } 
     return objindustry; 
    } 

的鉴于

任何ActionReslut创建一个方法将其返回类型是列表 和调用此方法
@Html.DropDownListFor(model => model.txtindustry, new SelectList(ViewBag.List, "Id", "industryName", 0)) 

请使用它你的问题可能会解决,

相关问题