0

In my MVC project when run and I press edit option in in view at that time this error occur

The ViewData item that has the key 'distic_id' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

In my view code

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

model is

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; }  
}
  • So? Check your Model if the compiler is correct. I guess it is. – nvoigt Mar 12 '14 at 07:10
  • Post your model type. – Ufuk Hacıoğulları Mar 12 '14 at 07:12
  • Give me your scenario where exactly error occur, if you want to appear City list or dist list in a drop down-list i can help you. – JegsVala Mar 12 '14 at 09:00
  • whene i choose update button in web grid at that time above error appear and update is press than choosen record will display in create form in texbox and dropdownlist – chirag kadia Mar 12 '14 at 09:24
  • Possible duplicate of [The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable'](http://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) –  Dec 21 '15 at 02:13

1 Answers1

0

if you want to get city or dist list in a drop down list please see the following code

1) Remove your code 2) Create one Model like this 3) if this drop down is used in more than one page CREATE ONE CONTROLLER like CommanController 4) write one method in this controller See Below code

First need to create Model like this

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

In Controller Two Step 1 is Create one method it return type is List and Call this method in any ActionReslut with the use of object

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

IN VIEW

@Html.DropDownListFor(model => model.txtindustry, new SelectList(ViewBag.List, "Id", "industryName", 0))

please use it your problem may be solve,

JegsVala
  • 1,789
  • 1
  • 19
  • 26