怎樣在Razor中使用HtmlHelper(MvcHtmlString)

來源:互聯網
上載者:User

如果你以前編寫的HtmlHelper喜歡返回string而非MvcHtmlString,那麼在使用Razor後要改改了。

 

假設原來有一個Helper調用:

                   <%= Html.ProgressBar(tree, progress) %>

而函式宣告是:
        public static string ProgressBar(this HtmlHelper htmlHelper, SFCProgressTree progressTree, SFCProgress progress, bool show = false)

{

...

return ImgTag.ToString()

}

則在新的Razor中除了在調用處改為:

                    @Html.ProgressBar(tree, progress)
外,還需要調整

        public static MvcHtmlString ProgressBar(this HtmlHelper htmlHelper, SFCProgressTree progressTree, SFCProgress progress, bool show = false)
{

...

return new MvcHtmlString(imgTag.ToString());
}

 

原因是Razor中將所有輸出的string都進行了Html編碼。

其實Html.ActionLink等輸出的都是MvcHtmlString,不過返回string 的Helper在aspx中也可以用。

但Razor中只能使用MvcHtmlString。

 

點擊下載免費的敏捷開發教材:《火星人敏捷開發手冊》

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.