This is a migrated thread and some comments may be shown as answers.

[Solved] MVC grid binding for Q3 release

4 Answers 148 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Anil
Top achievements
Rank 1
Anil asked on 05 Nov 2009, 02:28 PM

We have a ViewData class as follows:

public class QualifierRegistrationViewData

{

        public List<QualifierCompany> oQualifierCompany { get; set; }

        public List<QualifierNote> QualifierNotes { get; set; }


  [DisplayName("Open Permits")]

        public string txtOpenPermits { get; set; }

         [DisplayName("Expired Permits")]

        public string txtExpiredPermits { get; set; }

}

The QualifierNote entity has the following fields/properties:

QualifierNoteID

Note

Description

 

And this viewData is passed to the View as follows:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"

 Inherits="System.Web.Mvc.ViewPage<BuildingFrontDesk.ViewData.QualifierRegistrationViewData>" %>

<asp:Content contentPlaceHolderID="MainContent" runat="server">

<%= Html.Telerik().Grid(Model.QualifierNotes)

        .Name("Grid")

        .Columns(columns =>

            {

                  columns.Add(o => o. QualifierNoteID).Width(100);

                  columns.Add(o => o. Note).Width(200);

                  columns.Add(o => o. Description);

                          })

        .Pageable()

        .Sortable()

        .Scrollable()

        .Filterable()

%>

</asp:Content>

 

We are trying to bind QualifierNotes to the Grid. In the above code, in the columns.Add() we do not have access to the properties of the QualifierNotes entity. How to bind QualifierNotes entity inside strongly typed viewData to the Grid ?

All the samples provided by the documentation do not cover such scenerio.

In our case, the ViewData contains more complicated data structures.

4 Answers, 1 is accepted

Sort by
0
Yitzhak Khabinsky
Top achievements
Rank 1
answered on 05 Nov 2009, 06:55 PM

All of the Telerik’s MVC Grid samples and documentation are using simplistic scenario where the grid is the only one UI element on the Web page. It is not clear how to bind a grid for the scenario where page has other HTML controls besides the grid.


The corresponding viewdata class contains list of values (QualifierNote) for the Grid plus other values for the strongly typed model binding of the entire Web page.

In Anil's code sample, the Grid binding doesn’t have access to the QualifierNote fields: QualifierNoteID,  Note, Dsecription.

So how to use Telerik’s MVC Grid for such cases?

0
Atanas Korchev
Telerik team
answered on 06 Nov 2009, 09:16 AM
Hello,

The approach should be the same. You can configure the grid for binding using the following methods:

  1. Use the constructor which accepts IEnumerable<T>:

    Html.Telerik().Grid(Model.QualifierNotes)
  2. Use the overload which accepts view data key:
    Html.Telerik().Grid<QualifierNote>("QualifierNotes")
  3. Use the BindTo method:
    Html.Telerik().Grid<QualifierNote>()
                            .BindTo(Model.QualifierNotes)

In your case the type inference creates the grid as if the T is List<QualifierNote> instead of QualifierNote. Specifying the type explicitly should solve the problem:

Html.Telerik().Grid<BuildingFrontDesk.ViewData.QualifierNote>(Model.QualifierNotes)

It seems type inference didn't work as it was supposed to because QualifierNotes is List<T> rather than IEnumerable<T>. This makes the C# compiler pick the Grid<T>(T value) constructor rather than
Grid<T>(IEnumerable<T> dataSource).

I have attached a working project showing this approach.

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Yitzhak Khabinsky
Top achievements
Rank 1
answered on 09 Nov 2009, 04:13 PM

Hi Atanas,

Thank you very much for the tip on how to bind a grid in real world scenarios.

The grid binding is working well on our side now.

Just as a comment, it would an improvement to the Telerik Extensions for ASP.NET MVC if the type inference will work without explicit type casting for all 3 cases:

·         IEnumerable<T>

·         List<T>

·         IList<T>


Regards,
Yitzhak

 

0
Accepted
Atanas Korchev
Telerik team
answered on 09 Nov 2009, 04:15 PM
Hello Yitzhak Khabinsky,

I confirm that we would resolve this problem by removing the Grid (T item) method. Then type inference will work for all cases mentioned by you.

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Anil
Top achievements
Rank 1
Answers by
Yitzhak Khabinsky
Top achievements
Rank 1
Atanas Korchev
Telerik team
Share this question
or