问题描述
我正在尝试将 KendoUI DropDownListFor 用于我的模型外键并将其与 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!