thoughts on coding

March 4, 2009

Exploring new MVCContrib.Grid

Filed under: Uncategorized — Frantisek @ 9:40 pm

Last week there was published new Grid from MVCContrib project. (I’ve got the info from Jeremy Skinner’s comment to my previous blog). I’m happy that based on Jeremy’s comment all my extensions were added to the grid so it’s not necessary to add any new extensions. There is only one difference between current MVC grid and my extensions: using dictionary versus annonymous object. I must say using dictionary (Hash class) is easy to use and fits the requirements.

I must say, I like the design how is the grid done – there are clearly separated concerns:

  1. grid model – defines we want to display
  2. grid renderer – the special class used only for rendering the grid

Then there was created a pager (in previous version it was part of the grid and it’s separated now which is clean)

After downloading the source code I had to refactor my code (i.e. Attrs renamed to Attribute, etc.) which was very easy to be done.

Here is an simple which displays how the grid can be used:

<%=
   Html.Grid((IEnumerable<Organization>)Model.Items)
    .Columns(
              columns =>
              {
                  columns.For(item => Html.ActionLink<OrganizationController>(c => c.Edit(item.ID), " Edit ", new { Class = "none left" }))
                    .Named("").DoNotEncode();
                  columns.For(item => item.Name).Named(Html.OrderableHeader("Name") );
                  columns.For(item => item.URLName).Named(Html.OrderableHeader("Short Name", "URLName"));
                  columns.For(item => Html.ActionLink<OrganizationController>(
                                  c => c.Delete(item.ID),
                                  "<span class=\"redC\">X</span>", 
                                  new { id = "lnkDel", Class = "none left", title = "Delete", onclick = "Assure(event, 'Are you sure ?');"},
                                  LinkOptions.PlainContent))
                      .DoNotEncode()
                      .Named("Delete")
                      .Attributes(item => new Hash(Class => "w30 redC"));
              }
          )
     .Empty("There are no data")
     .Attributes(new Hash(Class => "list w50p", id => "mainGrid"))
     .RowAttributes(data => new Hash( id => "row", Class => "Prenatal {0}".FormatWith(data.IsAlternate ? "odd" : "") ))
    %>
    <%=Html.Pager(Model.Search.CurrentPage, Model.Search.MaxRowsPerPage, Model.Search.TotalRowCount)%>

I use my own implementation for Pager because my collections doesn’t imlpement IPagination so I can’t use the MVCContrib Pager.

Advertisements

11 Comments »

  1. Hi Frantisek

    I think you may have misunderstood how the grid/pager work. They are in no way tied to Linq to Sql.

    The Pager component interface (IPagination) which is used to determine the page number/page size. The default implementation of IPagination is LazyPagination which runs an in memory linq query (using skip/take) to created the paged data. This will work against *any* IEnumerable datasource irrespective of whether you’re using linq to sql, entity framework or just a plain in memory collection. There is no code in the grid/pager that uses linq to sql at all.

    Also, in your example the syntax for the pager is not quite right – it doesn’t take a page number/page size but rather an IPagination instance which contains this information.

    You might want to take a look at the Grid sample which is included in the MvcContrib release – you’ll notice that it is working completely in memory with no Linq to Sql anywhere to be seen 🙂

    Jeremy

    Comment by Jeremy Skinner — March 5, 2009 @ 12:56 pm

    • Hi Jeremy,

      yes, you’re right. I’m sorry, I was a little bit confused by the legacy grid. I’ll correct my post.

      As a pager I use my own paging because my datasource doesn’t implement IPagination.

      Anyway, thanks for the reply and Grid.
      Frantisek

      Comment by Frantisek — March 5, 2009 @ 1:05 pm

  2. Whoops, that second paragraph should have said:

    “The pager component USES AN interface (IPagination)…”

    Comment by Jeremy Skinner — March 5, 2009 @ 1:01 pm

    • Hi Jeremy,

      I can’t use standard Pager implementation because AsPagination(…) creates LazyPagination instance which converts the IEnumerable to IQueryable and when grid start to iterate over the items, it’s the grid who gives an hint to go for data through LazyPagination.

      I use NHibernate to get the data, so I can’t use the LazyPagination – in my case it’s the controller who gives the impuls to load the data a prepare the model. In IPagination case it’s in the rendering phase.

      I think IPagination is fine but I was thinking to implement it as a CustomPagination instead of LazyPaginantion.

      Jeremy, what do u think?

      Comment by Frantisek — March 5, 2009 @ 1:21 pm

  3. This is actually on my to-do-list to add already 🙂

    My plan was to add an additional implementation of IPagination (I was planning on calling it just ‘Pagination’) which works on a pre-paged set of data. There was a discussion on the mvccontrib mailing list about this a while back but I wanted to get the grid re-written before adding it. It should be straightforward to do so I’ll try and take a look at it sometime this week.

    Comment by Jeremy Skinner — March 5, 2009 @ 1:33 pm

    • Cool. Again, that was the reason why I tought that grids make the pagination using Linq. Anyway thanks.

      I extended OrderBy possibility of the grid using the simillar techniques as you did with the pager. I write a post about it today(I hope) so I’d like to ask you then for your comments and ideas.

      Thanks.
      Frantisek

      Comment by Frantisek — March 5, 2009 @ 1:38 pm

  4. Could you email me about your pager ?

    I have the same issue where my domain objects don’t implement IPagination and I’d like to try out what you’ve put together?

    Thanks

    Comment by Steve — March 5, 2009 @ 3:05 pm

  5. I’m also interested in the pager. Get same errors as Frantisek. 😦

    Comment by Pahe — October 26, 2009 @ 8:49 pm

  6. Hi Jeremy,

    Do we have this sourcecode available?. Is this a free ware?.
    How to implement Pagination and sorting without using Linq to sql with MvcContrib.Grid in MVC2 architecture?
    Can you pl suggest me any site so that i can go through that?
    Where can i find the complete code with which i want to explore?

    Thanks,
    Karthik

    Comment by Karthik — January 2, 2011 @ 3:27 pm

  7. Hi,

    Can Classic asp.net pages exist with MVC Pages in the same application?
    How to redirect aspx page to mvc page and vice versa?
    Any one experienced this issue?. Pl respond with an example.

    Thanks,
    Sunita

    Comment by Sunita — January 3, 2011 @ 10:50 am

  8. Thank you for a great post.

    Comment by toasty redhead — May 14, 2011 @ 8:27 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: