GridModel

2 views
Skip to first unread message

Amitabh

unread,
Jul 8, 2009, 4:54:15 AM7/8/09
to mvccontrib-discuss
Hi,

I am trying to create a hyperlink column in the Grid using the
separate GridModel syntext.

public class UserGridModel : GridModel<User>
{
public UserGridModel()
{
Column.For(u => u.Id);
Column.For(u => u.DisplayName);
Column.For(u => u.Email);
}

}

Please can you suggest how can I render DisplayName as Html.ActionLink
to a Controller Action.

Regards,
Amitabh

Jeremy Skinner

unread,
Jul 8, 2009, 5:20:41 AM7/8/09
to mvccontri...@googlegroups.com
Hi Amitabh,

I notice you posted the same question as a comment on my blog yesterday. For the benefit of the mailing list I'll post the solution here too.

One approach you can use is to take the HtmlHelper in the GridModel's constructor:


 public class UserGridModel : GridModel<User>
   {
       public UserGridModel(HtmlHelper html)
       {
           Column.For(u => u.Id);
           Column.For(u => html.ActionLink(u.DisplayName, "action", "controller")).DoNotEncode();
           Column.For(u => u.Email);
       }
   }


Then in your view you can pass the HtmlHelper to your GridModel:

<%= Html.Grid(users).WithModel(new UserGridModel(Html)) %>

Hope this helps,

Jeremy


2009/7/8 Amitabh <ranjan....@gmail.com>

Andre

unread,
Jul 8, 2009, 5:32:57 AM7/8/09
to mvccontrib-discuss
I get the following message when I do the html.ActionLink thing:
"Cannot convert lambda expression to type 'string' because it is not a
delegate type"
All references point to adding a "using System.Linq;", but that's
there. The problem seems to be with GridModel.

Jeremy Skinner

unread,
Jul 8, 2009, 5:37:21 AM7/8/09
to mvccontri...@googlegroups.com
You also need a using statement for System.Web.Mvc.Html.

Here is the sample code that I quickly put together which works correctly:

namespace Demo {
    using System.Web.Mvc;
    using MvcContrib.UI.Grid;
    using System.Web.Mvc.Html;

    public class User {
        public int Id { get; set; }
        public string DisplayName { get; set; }
        public string Email { get; set; }

    }

    public class UserGridModel : GridModel<User> {
        public UserGridModel(HtmlHelper html) {
            Column.For(u => u.Id);
            Column.For(u => html.ActionLink(u.DisplayName, "action", "controller"));
            Column.For(u => u.Email);
        }
    }
}

Jeremy



2009/7/8 Andre <an...@cyberdalexsp.com>

Andre

unread,
Jul 8, 2009, 5:57:38 AM7/8/09
to mvccontrib-discuss
Thanks. I seem to have left out (HtmlHelper html) which lead to other
errors, which caused the error as mentioned. All errors cleared
now :-)
> > > 2009/7/8 Amitabh <ranjan.amit...@gmail.com>

Jeremy Skinner

unread,
Jul 8, 2009, 6:08:23 AM7/8/09
to mvccontri...@googlegroups.com
Excellent - glad it's working for you.

Jeremy

2009/7/8 Andre <an...@cyberdalexsp.com>
Reply all
Reply to author
Forward
0 new messages