How do i Bind object in model from dropdownlist ?
public class Country
{
public int Id {get; set;}
public string Name{ get; set; }
}
public class City
{
public int Id {get; set;}
public string Name{ get; set; }
public Country Country {get; set;}
}
Controller ....
public ActionResult Create()
{
var model = new City();
ViewBag.Countries= ... //Loading all countries typeof IQuerable<Country>
return View(model);
}
cshtml....
@model Entities.City
@(Html.Kendo().DropDownListFor(m => m.Country)
.BindTo(ViewBag.Countries)
.DataValueField("Id")
.DataTextField("Name")
.OptionLabel("Please select."))
public ActionResult Create(City model)
{
if (ModelState.IsValid) /// <<== Error here..
{
var _obj = new City()
{
Name= model.Name,
Country = model.Country
};
}
Error is Model state is not valid. Because System.String cannot convert to Entities.Country
public class Country
{
public int Id {get; set;}
public string Name{ get; set; }
}
public class City
{
public int Id {get; set;}
public string Name{ get; set; }
public Country Country {get; set;}
}
Controller ....
public ActionResult Create()
{
var model = new City();
ViewBag.Countries= ... //Loading all countries typeof IQuerable<Country>
return View(model);
}
cshtml....
@model Entities.City
@(Html.Kendo().DropDownListFor(m => m.Country)
.BindTo(ViewBag.Countries)
.DataValueField("Id")
.DataTextField("Name")
.OptionLabel("Please select."))
public ActionResult Create(City model)
{
if (ModelState.IsValid) /// <<== Error here..
{
var _obj = new City()
{
Name= model.Name,
Country = model.Country
};
}
Error is Model state is not valid. Because System.String cannot convert to Entities.Country