@Html.DropDownListFor(m => m.Cities.CityId, new SelectList(Model.Drp_city , "Text", "Value"))
model binding in drop down list does not returning the id of selected item
-
04-10-2022 - |
Question
I am trying to bind the selected CityName
to my model but selected items id is not binding to my model variable
My model
public class EmployeeModel
{
public string name { get; set; }
public MyCity Cities { get; set; }
public List<SelectListItem> Drp_city { get; set; }
}
public class MyCity
{
public int CityId { get; set; }
public string CityName { get; set; }
}
Controller
empmodel.Drp_city = (from s in entity.Cities
select new SelectListItem()
{
Text = s.CityName,
Value = SqlFunctions.StringConvert((double)s.CityId)
}).ToList<SelectListItem>();
My View
@Html.DropDownListFor(model => model.Cities.CityId, Model.Drp_city)
I can see dropdown list of cities but selected city id is not getting stored in model.Cities.CityId when i try to store in database( NULL is stored in database )
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow