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

Bind Object from DropdownList

1 Answer 199 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Fatih
Top achievements
Rank 1
Fatih asked on 12 Oct 2012, 10:49 AM
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
Georgi Krustev
Telerik team
answered on 17 Oct 2012, 07:41 AM
Hello Fatih,

 
I will ask you to accomplish your task using a simple Html.DropDownListFor extension. Once you achieve your goal I will help you integrate Kendo DropDownList in your runnable project.

Regards,
Georgi Krustev
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
Georgi Krustev
Telerik team
Share this question
or