Extending Telerik client template column and getting the values passed through

admin

Administrator
Staff member
I am using the latest version
Code:
Telerik MVC controls
. I am using
Code:
ASP.NET MVC 3
with
Code:
razor
.

I have a grid that lists all of my grant applications. I am wanting to use a grid that loads these grant applications via AJAX. I also need to create a client template column that has action links. These action links can vary depending on the state of each grant application.

I worked through the article at: <a href="http://gedgei.wordpress.com/2011/07/02/telerik-mvc-grid-actionlink-column/" rel="nofollow">http://gedgei.wordpress.com/2011/07/02/telerik-mvc-grid-actionlink-column/</a>. I implemented the code as is and it works, I can create a client template column with a link in it. In my scenario I need to be able to pass in 2 parameters to the helper method, like:

Code:
column.ActionLink("Open", "Edit", "GrantApplication", item =&gt; new { id = item.Id, applicationStateId = item.GrantApplicationStateType.Id });

How I eventually implement this method in the end will change, but for now I am playing with these 2 input parameters to see how they are passed through and how I can retrieve them in the helper method.

The first question that I have regarding the article, why does the writer do the following:

Code:
var builder = factory.Template(x =&gt;
{
     var actionUrl = urlHelper.Action(action, controller, routeValues.Compile().Invoke(x));

     return string.Format(@"&lt;a href=""{0}""&gt;{1}&lt;/a&gt;", actionUrl, linkText);
});

I can only assume that this is the server side template that is created? But nothing displays in the grid, so how do I skip this part and go directly to the client template (this is what I actually need).

The following part is also confusing because when the first parameter (id) check comes through then it is of type
Code:
ParameterExpression
so it goes into the true part of the if, but when the second parameter (grant application state id) comes in then it is of another type (not sure what) so then it goes into the false part of the if statement:

Code:
switch (argument.NodeType)
{
     case ExpressionType.Constant:
          value = ((ConstantExpression)argument).Value;
          break;

     case ExpressionType.MemberAccess:
          MemberExpression memberExpression = (MemberExpression)argument;

          if (memberExpression.Expression is ParameterExpression)
               value = string.Format("&lt;#= {0} #&gt;", memberExpression.Member.Name);
          else
               value = GetValue(memberExpression);

          break;

     default:
          throw new InvalidOperationException("Unknown expression type!");
}

When the second paramenter values goes into the false part of the if statement it fails here:

Code:
value = GetValue(memberExpression);

..and gives the following error message which I have no idea what it is:

Code:
variable 'item' of type MyProject.ViewModels.GrantApplicationListViewModel' referenced from scope '', but it is not defined

Here is my view model:

Code:
public class GrantApplicationListViewModel
{
     public int Id { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string FullNameDisplay
     {
          get { return FirstName + " " + LastName; }
     }

     public DateTime CreatedDate { get; set; }

     public GrantApplicationStateType GrantApplicationStateType { get; set; }
}

Here is my partial grid declaration in my view:

Code:
@(Html.Telerik()
     .Grid&lt;GrantApplicationListViewModel&gt;()
     .Name("grdGrantApplications")
     .Columns(column =&gt;
     {
          column.Bound(x =&gt; x.Id)
               .ClientTemplate(
                    "&lt;label class=\"reference-number\"&gt;&lt;a href=\"" + @Url.RouteUrl(Url.GrantApplicationDetails("&lt;#= Id #&gt;")) + "\"&gt;" + "&lt;#= Id #&gt;" + "&lt;/a&gt;&lt;/label&gt;"
               )
               .Title("Ref #")
               .Width(70);

          column.Bound(x =&gt; x.FullNameDisplay)
               .Title("Owner")
               .Width(200);

          column.Bound(x =&gt; x.GrantApplicationStateType.Name)
               .Title("Status")
               .Width(90);

          //column.ActionLink("Edit", "Edit", "GrantApplication", item =&gt; new { id = item.Id });

          column.ActionLink("Open", "Edit", "GrantApplication", item =&gt; new { id = item.Id, applicationStateId = item.GrantApplicationStateType.Id });
     })
     .DataBinding(dataBinding =&gt; dataBinding.Ajax().Select("AjaxGrantApplicationsBinding", "Home"))
     .Pageable(paging =&gt; paging.PageSize(30))
     .TableHtmlAttributes(new { @class = "telerik-grid" })
)

What I am trying to achieve with the above is code is something to the effect of:

Code:
if grant application id  = 1

     then return Edit link and View link

else

     then return Details link

How would I do the above? Is the code in that article the only way to do it? Isn't there a more simplar way? I did Google and couldn't find much help on what I want to do. Has any one else come across something like this?