Dropdownlist создает исключение invalidoperationexception при проверке формы с помощью проверки модели в ASP.NET MVC
I am a fresher to ASP.net mvc and in Dropdownlist throws a InvalidOperationException while validating the form it throws a exception and i am using model validation in mvc why this error is occured plz help me soon as posssible!....
Что я уже пробовал:
Страница Контроллера:
[NonAction] public SelectList ToSelectList(DataTable table, string valueField, string textField) { List<SelectListItem> list = new List<SelectListItem>(); foreach (DataRow row in table.Rows) { list.Add(new SelectListItem() { Text = row[textField].ToString(), Value = row[valueField].ToString() }); } return new SelectList(list, "Value", "Text"); } public void source() { string Constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString; OdbcConnection con = new OdbcConnection(Constr); con.Open(); OdbcCommand cmd = new OdbcCommand("SELECT source_id,source_name,source_status FROM source_dets", con); OdbcDataAdapter da = new OdbcDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); ViewBag.source_rid = ToSelectList(dt, "source_id", "source_name"); } public void industry() { string Constr = System.Configuration.ConfigurationManager.ConnectionStrings["constr"].ConnectionString; OdbcConnection con = new OdbcConnection(Constr); con.Open(); OdbcCommand cmd = new OdbcCommand("SELECT industry_id,industry_name,industry_status FROM industry_dets", con); OdbcDataAdapter da = new OdbcDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); ViewBag.industry_rid = ToSelectList(dt, "industry_id", "industry_name"); }
обзорная страница :
@Html.DropDownListFor(a => a.source, ViewBag.source_rid as SelectList, "--Please Select--", new { @class = "form-control bg-light" })
Бросать исключение :
System.InvalidOperationException: 'There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'industry'.'