How should I write this so that Razor does not escape all the bits and bobs:
Helper class (ironically not that helpful)
public static class TableHeaderSortingHelpers
{
public static string SortTableClickEvent(this HtmlHelper html, string url, string column)
{
string sortingPropertiesObject;
sortingPropertiesObject = "var properties = new James.prototype.Table.SortingProperties();";
sortingPropertiesObject += "properties.url = '" + url + "';";
sortingPropertiesObject += "properties.colName = '" + column + "';";
sortingPropertiesObject += "onclick = 'James.Table.SortByColumn(properties, this);'";
return sortingPropertiesObject ;
}
}
Razor view
<table class="table table-striped">
<tr>
<th width="100%" @Html.SortTableClickEvent(@Request.Path, "Name");>
Name
</th>
After compiling it looks like this:
<th width="100%" this);';="" 'name';onclick="'James.Table.SortByColumn(properties," =="" james.prototype.table.sortingproperties();properties.url="'/Site/List';properties.colName" properties="new" var="">
Name
</th>
EDIT //////////////////////
If I try to return an MVCHtmlString I get the following:
public static class TableHeaderSortingHelpers
{
public static MvcHtmlString SortTableClickEvent(this HtmlHelper html, string url, string column)
{
string sortingPropertiesObject;
sortingPropertiesObject = "var properties = new James.prototype.Table.SortingProperties();";
sortingPropertiesObject += "properties.url = '" + url.ToString() + "';";
sortingPropertiesObject += "properties.colName = '" + column + "';";
sortingPropertiesObject += "onclick = 'James.Table.SortByColumn(properties, this);'";
MvcHtmlString returnString = new MvcHtmlString(sortingPropertiesObject);
return returnString;
}
}
Output
<th width="100%" ;="" ;onclick="James.Table.SortByColumn(properties, this);" ;properties.colname="Name" james.prototype.table.sortingproperties();properties.url="/Site/List" properties="new" var="">
Name
</th>