Html.ActionLink and ClientTemplate

4 posts, 0 answers
  1. Vlad
    Vlad avatar
    18 posts
    Member since:
    Sep 2012

    Posted 09 Oct 2012 Link to this post

    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.
  2. Vlad
    Vlad avatar
    18 posts
    Member since:
    Sep 2012

    Posted 09 Oct 2012 Link to this post

    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#")));
    )
  3. Kendo UI is VS 2017 Ready
  4. jose
    jose avatar
    8 posts
    Member since:
    Apr 2012

    Posted 27 Dec 2012 Link to this post

    The example in this page answer this question. 
    http://docs.kendoui.com/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/faq
  5. David A.
    David A. avatar
    126 posts
    Member since:
    Aug 2008

    Posted 14 Jan 2013 Link to this post

    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" })


Back to Top
Kendo UI is VS 2017 Ready