【问题标题】:How to bind KendoUI DropDownListFor to ViewData or ViewBag?如何将 KendoUI DropDownListFor 绑定到 ViewData 或 ViewBag?
【发布时间】:2013-01-01 05:24:02
【问题描述】:

我正在尝试将 KendoUI DropDownListFor 用于我的模型外键并将其与 ViewData/ViewBag 完整列表绑定,但似乎无法正常工作,我是否遗漏了什么?

@(Html.DropDownListFor(model => model.Hotel.HotelStatusId, ViewData["HotelStatuses"] as SelectList))

这似乎可行,但需要我创建一个视图模型。

@(Html.Kendo().DropDownListFor(model => model.Hotel.HotelStatusId)
                              .BindTo(Model.HotelStatuses)
                              .OptionLabel("select hotel status...")
                              )

我避免使用 viewmodel,因为我需要将数据提交回 ASP MVC。使用自定义视图模型,我无法正确绑定它。

【问题讨论】:

  • 如果你强制转换而不是使用 AS 操作符会抛出异常吗?
  • @Pechka,不,它不会抛出异常。
  • 说实话我当时不知道

标签: asp.net-mvc kendo-ui


【解决方案1】:

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") 

【讨论】:

  • 这行得通,但我必须先设置 DataTextField。 .BindTo(ViewData["HotelStatuses"] as SelectList).DataTextField("Text")
  • 我不得不使用.BindTo(ViewBag.HotelStatuses)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多