Bind Object from DropdownList

2 posts, 0 answers
  1. Fatih
    Fatih avatar
    7 posts
    Member since:
    Sep 2012

    Posted 11 Oct 2012 Link to this post

    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
  2. Sebastian
    Admin
    Sebastian avatar
    9934 posts

    Posted 23 Nov 2012 Link to this post

    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!
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top