Skip to content

Instantly share code, notes, and snippets.

@krishnabharath
Forked from robertmclaws/KendoMvcExtensions.cs
Created November 12, 2013 13:22
Show Gist options
  • Save krishnabharath/7430720 to your computer and use it in GitHub Desktop.
Save krishnabharath/7430720 to your computer and use it in GitHub Desktop.
TO USE:
Simply tack on .ToMvcClientTemplate() to the end of the outer-most MVC helper
that you're trying to turn into a template.
In this example: http://demos.kendoui.com/web/grid/detailtemplate.html, you
would add it after the last ToClientTemplate() that occurs before the script
section with the "function databound()" handler.
Note that returning an HtmlString instead of an MvcHtmlString will ensure you
don't have to call @Html.Raw() in your template to bypass the AntiXss encoding
that will be performed when MvcHtmlString.ToString() is called.
Feel free to lean hard on Telerik to get their ToClientTemplate() code changed
to include this technique.
using Kendo.Mvc.UI;
using System.Web;
using System.Web.Mvc;
using System.Web.Security.AntiXss;
using System.Web.Util;
namespace Your.Namespace.Here
{
public static class KendoMvcExtensions
{
public static IHtmlString ToMvcClientTemplate(this MvcHtmlString mvcString)
{
if (HttpEncoder.Current.GetType() == typeof (AntiXssEncoder))
{
var initial = mvcString.ToHtmlString();
var corrected = initial.Replace("\\u0026", "&").Replace("%23", "#").Replace("%3D", "=").Replace(" ", " ");
return new HtmlString(corrected);
}
return mvcString;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment