narlified我應該怎麼寫這使剃刀不逃避所有的位和鮑勃:我的HTML助手是由剃刀
Helper類(具有諷刺意味的不是有用)
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視圖
<table class="table table-striped">
<tr>
<th width="100%" @Html.SortTableClickEvent(@Request.Path, "Name");>
Name
</th>
編譯之後,它看起來是這樣的:
<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>
編輯//////////////////////
如果我試圖返回MVCHtmlString我得到的以下:
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;
}
}
輸出
<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>
你可以發佈你的預期輸出的例子嗎?我問的原因是因爲'var properties = ...'會直接在你的'th'標籤中結束。 –