如何KendoUI DropDownListFor绑定ViewData的或ViewBag? [英] How to bind KendoUI DropDownListFor to ViewData or ViewBag?
问题描述
我试图用KendoUI DropDownListFor为我的模型ForeignKey的和ViewData的/ 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...")
)
我使用的视图模型,因为我需要将数据提交回ASP MVC避免。通过自定义视图模型,我无法正确绑定。
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.
推荐答案
Viewbag /的ViewData可以在控制器中充满这样的:
Viewbag/ViewData can be filled like this in controller:
ViewData["HotelStatuses"] =
new SelectList(db.HotelStatuses, "HotelStatusId", "HotelStatusText");
和考虑您可以使用的ViewData / ViewBag:
And in view you can use ViewData/ViewBag:
@(Html.Kendo().DropDownListFor(model => model.Hotel.HotelStatusId)
.BindTo(ViewData["HotelStatuses"] as SelectList))
.DataTextField("Text")
这篇关于如何KendoUI DropDownListFor绑定ViewData的或ViewBag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!