asp.net-mvc 如何将 KendoUI DropDownListFor 绑定到 ViewData 或 ViewBag?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14108019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to bind KendoUI DropDownListFor to ViewData or ViewBag?
提问by JeeShen Lee
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?
我正在尝试将 KendoUI DropDownListFor 用于我的模型外键并将其与 ViewData/ViewBag 完整列表绑定,但似乎无法正常工作,我错过了什么吗?
@(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.
我避免使用视图模型,因为我需要将数据提交回 ASP MVC。使用自定义视图模型,我无法正确绑定它。
回答by Bahman
Viewbag/ViewData can be filled like this in controller:
Viewbag/ViewData 可以像这样在控制器中填充:
ViewData["HotelStatuses"] =
new SelectList(db.HotelStatuses, "HotelStatusId", "HotelStatusText");
And in view you can use ViewData/ViewBag:
在视图中,您可以使用 ViewData/ViewBag:
@(Html.Kendo().DropDownListFor(model => model.Hotel.HotelStatusId)
.BindTo(ViewData["HotelStatuses"] as SelectList))
.DataTextField("Text")

