2014-03-12 29 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)) 

請使用它你的問題可能會解決,

相關問題