This is a migrated thread and some comments may be shown as answers.

Bind Object from DropdownList

1 Answer 340 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Fatih
Top achievements
Rank 1
Fatih asked on 11 Oct 2012, 11:26 PM
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

1 Answer, 1 is accepted

Sort by
0
Sebastian
Telerik team
answered on 23 Nov 2012, 02:06 PM
Hello Fatih,

 
See my colleague's reply in this forum thread.

Regards,
Sebastian
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
DropDownList
Asked by
Fatih
Top achievements
Rank 1
Answers by
Sebastian
Telerik team
Share this question
or