user3349049
user3349049

Reputation: 172

Kendo Grid unable to populate view

I am trying to populate a kendo ui grid with data from my database. I cannot find a answer with all the other postings with the same issue. the user does a search using UserId, and To/From Dates. using breakpoints i can see the data coming back through the controller. first time using kendo so if i need to post more information please let me know

Error Message

Compiler Error Message: CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type

Source Error:

Line 11:     .Columns(columns =>
Line 12:     {
Line 13:         columns.Bound(m => m.ActivityDate).Format("{0:MM/dd/yyyy}");
Line 14:         columns.Bound(m => m.Assignment);
Line 15:         columns.Bound(m => m.Action);

Controller

[HttpPost]
public ActionResult ReportsPhoneSupport(ReportsPhoneSupport model)
{       
    string[] userIds = model.UserId.Split(',');

    foreach (string userId in userIds)
    {
        int iUserId = 0;
        if (Int32.TryParse(userId, out iUserId))
        {
            ReportPhoneSupportResultTypedView results = new ReportPhoneSupportResultTypedView();
            RetrievalProcedures.FetchReportPhoneSupportResultTypedView(results, model.FromDate, model.ToDate, iUserId);
        }
    }

    return View(model);
}

View

@{
    ViewBag.Title = "ReportsPhoneSupport";
 }

<h2>ReportsPhoneSupport</h2>

@(Html.Kendo().Grid<UtilityWebSite.Controllers.ReportsPhoneSupportController>()
    .Name("grid")
    .Columns(columns =>
    {
        columns.Bound(m => m.ActivityDate).Format("{0:MM/dd/yyyy}");
        columns.Bound(m => m.Assignment);
        columns.Bound(m => m.Action);
        columns.Bound(m => m.ToFrom);
        columns.Bound(m => m.Result);
        columns.Bound(m => m.Description);
    })
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
    .HtmlAttributes(new { style = "height:430px;" })
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(20)
        .Read(read => read.Action("ReportsPhoneSupport", "ReportsPhoneSupport"))
    )
)

CLASS

 public class ReportsPhoneSupport
{
    public string UserId { get; set; }
    public DateTime ToDate { get; set; }
    public DateTime FromDate { get; set; }

}

}Data From SQL

Upvotes: 1

Views: 2140

Answers (1)

David Shorthose
David Shorthose

Reputation: 4497

Well you need to be returning Json to the grid and not a view.

So something like

Public jsonresult readData([DataSourceRequest] DataSourceRequest request, MyModel model)
{
 Do something here to get data for source
 Eg List<MyReturnedModel> returnModel = new List<MyReturnedModel>()
 { new MyReturnedModel(){ id = 1}, (etc)
  };

Return json(returnedModel.ToDataSourceResult(request,ModelState), jsonbehaviour.Get);
}

Hopefully this will give you enough of an idea to run with.

Edit:

I also noticed you are setting the controller class as the grid entity when it should be your model class.

second edit:

ok so if I have this right the actual data being shown on the grid should be of Type ReportPhoneSupportResultTypedView

so if you change the grid first to this:

@(Html.Kendo().Grid<**ReportPhoneSupportResultTypedView**>()
 (include any namespaces here for the model type to be referenced correctly.)
 .Name("grid")
.Columns(columns =>
{
    columns.Bound(m => m.ActivityDate).Format("{0:MM/dd/yyyy}");
    columns.Bound(m => m.Assignment);
    columns.Bound(m => m.Action);
    columns.Bound(m => m.ToFrom);
    columns.Bound(m => m.Result);
    columns.Bound(m => m.Description);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.HtmlAttributes(new { style = "height:430px;" })
.DataSource(dataSource => dataSource
    .Ajax()
    .PageSize(20)
    .ServerOperations(true)
   .Read(read => read.Action("ReportsPhoneSupport", "ReportsPhoneSupport").Data("MyFilters"))
)

)

You will notice I added the .Data after your read action and using some javascript you can add your search parameters

so something like this:

<script>
function MyFilters()
{
     return { filterModel: {   
                               UserId: 1,
                               ToDate: "01 Jan 2014",
                               FromDate: "20 Jan 2014"
                            }
            };
}
</script>

This then should construct the model object you are passing back to perform you filtering and then change you read action to something like this:

Note Make sure you include the namespace Kendo.Mvc.Extensions and Kendo.Mvc.UI into the controller for the DataSourceRequest/ toDataSourceResult to be picked up correctly.

public JsonResult ReportsPhoneSupport([DataSourceRequest] DataSourceRequest request,ReportsPhoneSupport filterModel )
{       
   string[] userIds = filterModel.UserId.Split(',');

foreach (string userId in userIds)
{
    int iUserId = 0;
    if (Int32.TryParse(userId, out iUserId))
    {
        ReportPhoneSupportResultTypedView results = new ReportPhoneSupportResultTypedView();
        RetrievalProcedures.FetchReportPhoneSupportResultTypedView(results, filterModel.FromDate, filterModel.ToDate, iUserId);
    }
}
  **I am assuming your results set is some form of enumerable/IList**
return Json( results.toDataSourceResult(request,ModelState), jsonbehaviour.AllowGet);

}

Hopefully this expanded example should give you the answer you are looking for.

Upvotes: 1

Related Questions