JeeShen Lee
JeeShen Lee

Reputation: 3816

How to bind KendoUI DropDownListFor to ViewData or ViewBag?

I'm trying to use KendoUI DropDownListFor for my model foreignkey and bind it with ViewData/ViewBag complete list but can't seems to work, am i missing something?

@(Html.DropDownListFor(model => model.Hotel.HotelStatusId, ViewData["HotelStatuses"] as SelectList))

This seems to work but required me to create a viewmodel.

@(Html.Kendo().DropDownListFor(model => model.Hotel.HotelStatusId)
                              .BindTo(Model.HotelStatuses)
                              .OptionLabel("select hotel status...")
                              )

I'm avoiding using viewmodel because i need to submit the data back to ASP MVC. With the custom viewmodel, i couldn't bind it correctly.

Upvotes: 6

Views: 24643

Answers (1)

Bahman
Bahman

Reputation: 1064

Viewbag/ViewData can be filled like this in controller:

ViewData["HotelStatuses"] = 
new SelectList(db.HotelStatuses, "HotelStatusId", "HotelStatusText");

And in view you can use ViewData/ViewBag:

 @(Html.Kendo().DropDownListFor(model => model.Hotel.HotelStatusId)
 .BindTo(ViewData["HotelStatuses"] as SelectList))
 .DataTextField("Text") 

Upvotes: 9

Related Questions