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

DropdownList Model Binding - Unable to get it working

1 Answer 334 Views
DropDownList
This is a migrated thread and some comments may be shown as answers.
Uday
Top achievements
Rank 1
Uday asked on 04 Aug 2014, 05:32 PM
Hi everyone,

I am having hard time getting the model binding work with the DropdownList.

* I am not manually setting the name on the widget
* I do not have AutoBind set to false 

@(Html.Kendo().DropDownListFor(m=> m.SelectedProduct)
          .HtmlAttributes(new { style = "width: 250px" })
          .DataTextField("ProductName")
          .DataValueField("ProductID")
          .DataSource(source =>
          {
              source.Read(read =>
              {
                  read.Action("GetProducts", "DropdownList");
              });
          })
    )

This is from the telerik demos.

Here is the Controller Code:
public ActionResult RemoteDataSource()
       {
           var model = new RemoteDataSourceViewModel()
           {
               SelectedProduct = new SelectedProduct()
               {
                   ProductID = 7,
                   ProductName = "Uncle Bob's Organic Dried Pears"
               }
           };
            
           return View(model);
       }

The above code would load the dropdownlist, but doesn't set the value on initial load.

I looked through previous post and could not find why this wouldn't work.

Any suggestions on what I might be doing wrong?

I appreciate your time.

Uday

1 Answer, 1 is accepted

Sort by
0
Accepted
Alexander Popov
Telerik team
answered on 06 Aug 2014, 12:19 PM
Hi Uday,

This happens because the DropDownList's value should be an integer matching the ProductID. Using the ProductID property (instead of a complex object) as an argument for the DropDownListFor helper should do the trick. 

Regards,
Alexander Popov
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
Tags
DropDownList
Asked by
Uday
Top achievements
Rank 1
Answers by
Alexander Popov
Telerik team
Share this question
or