Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 615
JeeShen Lee Avatar asked Jan 01 '13 05:01

JeeShen Lee


1 Answers

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") 
like image 69
Bahman Avatar answered Nov 15 '22 06:11

Bahman