How to bind KendoUI DropDownListFor to ViewData or ViewBag?

巧了我就是萌 提交于 2019-12-23 09:34:53

问题


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.


回答1:


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") 


来源:https://stackoverflow.com/questions/14108019/how-to-bind-kendoui-dropdownlistfor-to-viewdata-or-viewbag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!