LoftyTowers
LoftyTowers

Reputation: 573

Can not populate Kendo grid using a ViewBag instead of a model

I have created a MVC application that uses multiple entities to populate a view using a ViewBag to pass the data.

However I can not seem to get the Kendo UI grid to populate from the ViewBag and I keep getting the following runtime error:

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

I believe the issue I am having is from this line of code:

@(Html.Kendo().Grid((Dictionary<string,string>)ViewBag.ApplicationStatuses)

The full Razor code is as follows:

@{
    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>
            );
        })
    )

My controller:

 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();
    }

Could anybody explain to me why I am having this issue and what the solution is?

Any help would be very appreciated.

Upvotes: 1

Views: 1956

Answers (2)

LoftyTowers
LoftyTowers

Reputation: 573

Sorry for the late answer, but I Solved this by changing the logic of my program and using a Model instead. This populated the Grid and gave me easier control over validation. Thank you for your patience.

Upvotes: 1

Andzej Maciusovic
Andzej Maciusovic

Reputation: 4476

First of all, done use ViewBag. Create strongly typed viewmodel see this http://www.asp.net/mvc/tutorials/views/dynamic-v-strongly-typed-views

and you can't bind dictionary to kendo like this

Upvotes: 0

Related Questions