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

Html.ActionLink and ClientTemplate

3 Answers 1216 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vlad
Top achievements
Rank 1
Vlad asked on 09 Oct 2012, 07:47 PM
Hi,

I am trying to create hyperlink column using ClientTemplate and Html.ActionLink.  Here is my code:

@(Html.Kendo().Grid<OrderViewModel>()
      .Name("Orders")
      .HtmlAttributes(new {style = "height: 400"})
      .Columns(c =>
          {
              c.Bound(p => p.Id).Title("Order ID")
                  .Groupable(false)
                  .ClientTemplate((@Html.ActionLink("#=Id#", "Index", "Splits", new { orderId = "#=Id#" }, null).ToHtmlString()));
})

It creates properly hyperlink for id in the browser, but when I click on hyperlink I am getting on the server orderId "#=Id#", so first "#=Id#" was rendered properly with proper order Id, but it was not rendered when it passed to the server side.

Please advise what I am doing wrong.

Thanks.

3 Answers, 1 is accepted

Sort by
0
Vlad
Top achievements
Rank 1
answered on 09 Oct 2012, 11:55 PM
The problems is with RouteValues escaping the string, so I end up using Replace method, to change escaped values to #=Id# value back.

@(Html.Kendo().Grid<OrderViewModel>()
      .Name("Orders")
      .HtmlAttributes(new {style = "height: 400"})
      .Columns(c =>
          {
              c.Bound(p => p.Id).Title("Order ID")
                  .Groupable(false)
                  .ClientTemplate((@Html.ActionLink("#=Id#", "Index", "Splits", new { Id = "OrderId" }, null).ToHtmlString().Replace("OrderId", "#=Id#")));
)
0
jose
Top achievements
Rank 1
answered on 27 Dec 2012, 10:05 PM
The example in this page answer this question. 
http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/faq
0
David A.
Top achievements
Rank 1
answered on 15 Jan 2013, 02:50 AM
I use an htmlhelper method for this:
public static string KendoGridTemplateLink(this HtmlHelper helper, string linkText, string actionName, string controllerName, object routeValues = null,  object htmlAttributes = null)
        {
            var anchor = new TagBuilder("a");
            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection);
            var queryBuilder = new StringBuilder();
 
            anchor.MergeAttributes(new RouteValueDictionary(htmlAttributes));
            // use urldecode to unescape
            anchor.MergeAttribute("href", HttpUtility.UrlDecode(urlHelper.Action(actionName, controllerName, routeValues)));
 
             
            anchor.InnerHtml = linkText;
 
            return anchor.ToString(TagRenderMode.Normal);
 
        }
and call it like this:
@Html.KendoGridTemplateLink("#=kendo.toString(NarrativeDate, 'MM/dd/yyyy h:mm tt')#", "Narrative", "Narratives", new { area = "Application", id="#=NarrativeId#" }, new { @class = "perma-link" })


Tags
Grid
Asked by
Vlad
Top achievements
Rank 1
Answers by
Vlad
Top achievements
Rank 1
jose
Top achievements
Rank 1
David A.
Top achievements
Rank 1
Share this question
or