2014-01-06 59 views
2

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);&#39;;="" &#39;name&#39;;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> 
+2

你可以發佈你的預期輸出的例子嗎?我問的原因是因爲'var properties = ...'會直接在你的'th'標籤中結束。 –

回答

4

嘗試返回MvcHtmlStringHtmlString應防止剃刀的逃逸機制

public static IHtmlString 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 new HtmlString(sortingPropertiesObject); 
} 

更新:

的可能,而不是試圖實例屬性對象(如果你不把它不會內聯工作一切都在一行)。只需使用普通的json對象並調用你的方法即可。 助手可能看起來像這樣的例子:

public static IHtmlString SortTableClickEvent(this HtmlHelper html, string url, string column) 
{ 
    string sortingPropertiesObject = string.Format(
     "onclick = \"James.Table.SortByColumn({{ url:'{0}', column:'{1}' }}, this);\"" 
     , url, column); 

    return new HtmlString(sortingPropertiesObject); 
} 

或者,如果您希望自定義屬性對象在JavaScript中,只是把所有東西都在同一行,正如我所說的,正確逃生一切使的onclick方法以及格式化,不打破你的HTML標記...

+0

你能否再次檢查我的OP,我加了一點。您的示例給編譯錯誤:錯誤不能隱式地將類型'字符串'轉換爲'System.Web.HtmlString' – Jimmyt1988

+0

這是另一個問題,請刪除您的編輯,將其標記爲回答並添加另一個問題。 除此之外。您正試圖將所有內容放入內聯的onclick句柄中。無論如何,這是非常糟糕的做法,我不會那麼做 – MichaC

+0

這不是一個不同的問題brah ...您的建議很不幸不起作用...而且我所做的並不是不好的做法。我已經逐字地宣佈了一個新屬性,可以放入我正在調用的一個類中。我需要一個單獨的屬性對象爲每個電話。考慮它是表頭。 – Jimmyt1988

1

我同意MichaC,它是不好的做法,你正在嘗試做什麼。我提出這個問題,在視圖內部保留邏輯。

的HTML:

<th width="100%" class="sort-table" data-column="Name"> 
    Name 
</th> 

和JavaScript,這必須是視圖中。

<script type="text/javascript"> 
$(function() { 
    $('.sort-table').click(function() {    
     // Get the value from the data-column attribute. 
     var column = $(this).data('column'); 

     // Create object and set properties. 
     var properties = new James.prototype.Table.SortingProperties(); 
     properties.url = '@Request.Path'; 
     properties.colName = column; 

     // Sort the table. 
     James.Table.SortByColumn(properties, this); 
    }); 
}); 
</script> 

但是,我不知道該click事件可以綁定到一個<th>元素。

+0

感謝您的幫助兄弟。非常感謝 – Jimmyt1988