如何KendoUI DropDownListFor绑定ViewData的或ViewBag? [英] How to bind KendoUI DropDownListFor to ViewData or ViewBag?

查看:871
本文介绍了如何KendoUI DropDownListFor绑定ViewData的或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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆