Asp.net mvc 如何将KendoUI DropDownListFor绑定到ViewData或ViewBag?
我试图使用KendoUI DropDownListFor作为我的模型外键,并将其与ViewData/ViewBag完整列表绑定,但似乎无法工作,我是否遗漏了什么Asp.net mvc 如何将KendoUI DropDownListFor绑定到ViewData或ViewBag?,asp.net-mvc,kendo-ui,Asp.net Mvc,Kendo Ui,我试图使用KendoUI DropDownListFor作为我的模型外键,并将其与ViewData/ViewBag完整列表绑定,但似乎无法工作,我是否遗漏了什么 @(Html.DropDownListFor(model => model.Hotel.HotelStatusId, ViewData["HotelStatuses"] as SelectList)) 这似乎可行,但需要我创建一个viewmodel @(Html.Kendo().DropDownListFor(model =&g
@(Html.DropDownListFor(model => model.Hotel.HotelStatusId, ViewData["HotelStatuses"] as SelectList))
这似乎可行,但需要我创建一个viewmodel
@(Html.Kendo().DropDownListFor(model => model.Hotel.HotelStatusId)
.BindTo(Model.HotelStatuses)
.OptionLabel("select hotel status...")
)
我避免使用viewmodel,因为我需要将数据提交回ASP MVC。使用自定义viewmodel时,我无法正确绑定它。Viewbag/ViewData可以在控制器中这样填充:
ViewData["HotelStatuses"] =
new SelectList(db.HotelStatuses, "HotelStatusId", "HotelStatusText");
在视图中,您可以使用ViewData/ViewBag:
@(Html.Kendo().DropDownListFor(model => model.Hotel.HotelStatusId)
.BindTo(ViewData["HotelStatuses"] as SelectList))
.DataTextField("Text")
如果您使用AS运算符而不是强制转换它,它是否会引发异常?@Pechka,不,它不会引发异常。老实说,我不知道这是否有效,但我必须先设置DataTextField。BindTo(ViewData[“HotelStatuses”]作为SelectList)。DataTextField(“Text”)我必须使用
。BindTo(ViewBag.HotelStatuses)