I am using the MVC Contrib Grid for rendering, sorting and filtering my data grid, but I am having problems now that it has been upgraded for MVC3 and Razor. The Custom method in my columns collection does not work for aspx pages , and the columns Action method is now obsolete.
I use grids action method to render my columns like this in MVC 2:
...
<% Html.Grid(Model).Columns(column => {
column.For(x => x.Id);
column.For(x => x.Name);
column.For(x => x.Email);
column.For(x => x.DateOfBirth);
column.For("View User").Named("Tools").Action(x => { %>
<td>
<%= Html.ActionLink("View", "View", new { id = p.Id })%>
<%= Html.ActionLink("Edit ", "Edit", new { id = p.Id })%>
//Snip as there are too many tools :-)
//.....
<%= Html.ActionLink("Delete", "Delete", new { id = p.Id })%>
</td>
<% });
...
Now with the latest version there is a Custom method that replaces the obsolete Action method. I looked at how it can be done in here, which basically works for me now, but I loose all my helpers in the aspx view (url, etc), and now need to render my contents in another method in my model like below:
...
<% Html.Grid(Model).Columns(column => {
column.For(x => x.Id);
column.For(x => x.Name);
column.For(x => x.Email);
column.For(x => x.DateOfBirth);
//the new custom column
column.Custom(Model.ToolsRenderer);
<% });
...
The grid model method below called ToolsRenderer is used to render my html string.
public UserManagementViewModel : BaseModel {
//..snip
//
public object ToolsRenderer(Client client)
{
List<string> links = new List<string>();
var editLink = new TagBuilder("a");
// here is my biggest problem, before Html.ActionLink used to make
// sure that I don't have any missing links or help me if i need to
// refactor an action / controller name :-(
editLink.Attributes["href"] = GetEditUserLink(client, HttpContext.Current.Request.Url.AbsoluteUri);
editLink.SetInnerText("edit");
links.Add(editLink.ToString());
...
...lots of links to be generated here
...
return MvcHtmlString.Create(string.join(" |", links))
}
//..snip
}
This works for now, but is there a way to get my aspx page working like the razor views like below?
@Html.Grid(Model).Columns(column =>
{
column.For(x => x.Id).
column.For(x => x.Name);
column.Custom(@<td><a href='@Html.Actionlink("edit","user",new {id})' alt="@item.email"/><a></td>)
})
I want to say something like:
...
column.Custom(%><td><a href='<%=Html.Actionlink("edit","user",new {id})%>' alt="<%=item.email%>"/><a></td><%)
...
Finally managed to find a work around with some help and digging, and instead of using the custom column, use column.for and push in the Html.Partial. Something like the code below.
<% Html.Grid(Model).Columns(column => {
column.For(x => x.Id);
column.For(x => x.Name);
column.For(x => x.Email);
column.For(x => x.DateOfBirth);
// could use RenderPartial() to
column.For(x =>Html.Partial("UserActionColumn", x));
<% });