无法使用ViewBag代替模型填充剑道网格
本文关键字:填充 模型 网格 ViewBag | 更新日期: 2023-09-27 18:25:07
我创建了一个MVC应用程序,该应用程序使用多个实体来填充视图,并使用ViewBag传递数据。
然而,我似乎无法从ViewBag中填充Kendo UI网格,并且我一直收到以下运行时错误:
CS0452: The type 'System.Collections.Generic.KeyValuePair<string,string>' must be a reference type in order to use it as parameter 'T' in the generic type or method 'Kendo.Mvc.UI.Fluent.WidgetFactory.Grid<T>(System.Collections.Generic.IEnumerable<T>)'
我相信我遇到的问题来自这行代码:
@(Html.Kendo().Grid((Dictionary<string,string>)ViewBag.ApplicationStatuses)
完整的Razor代码如下:
@{
ViewBag.Title = "TestView";
}
<h2>TestView</h2>
@(Html.Kendo().Grid((Dictionary<string,string>)ViewBag.ApplicationStatuses).Name("UserDetails").Columns(c =>
{
c.Bound(ud => ud.FullName);
c.Bound(ud => ud.JobTitle);
c.Bound(ud => ud.Department);
c.Bound(ud => ud.Email);
c.Template(
@<text>
@Html.ActionLink("User Details", "UserDetails", "User", new { userName = item.UserName }, null)
</text>
);
})
)
我的控制器:
public ActionResult TestView(string userName)
{
Dictionary<string, string> statuses = new Dictionary<string, string>();
foreach (KeyValuePair<Application.Applications, IUser> entry in Application.UserMap)
{
IUser user = entry.Value;
statuses.Add(entry.Key.ToString(), entry.Value.GetUserStatus(userName));
}
ViewBag.ApplicationStatuses = statuses;
ViewBag.UserName = userName;
return View();
}
有人能向我解释为什么我会遇到这个问题以及解决方案是什么吗?
如有任何帮助,我们将不胜感激。
很抱歉回答得太晚了,但我通过更改程序的逻辑并使用模型来解决这个问题。这填充了网格,使我更容易控制验证。谢谢你的耐心。
首先,使用ViewBag。创建强类型视图模型请参阅http://www.asp.net/mvc/tutorials/views/dynamic-v-strongly-typed-views
你不能像这个那样将dictionary绑定到剑道