开发者

HtmlHelper.Button(..., string onClickMethod, ...) HTML-encodes single-quotes!

开发者 https://www.devze.com 2023-02-09 19:45 出处:网络
I\'m upgrading an old project from MVC 1.0 to MVC 3.0 (yes, it\'s that old), and I\'ve run into an issue where calling HtmlHelper.Button(..., onClickMethod, ...) HTML-encodes single quotes into &#

I'm upgrading an old project from MVC 1.0 to MVC 3.0 (yes, it's that old), and I've run into an issue where calling HtmlHelper.Button(..., onClickMethod, ...) HTML-encodes single quotes into '

I can see how this would not be an issue if onClickMethod was just the name of a method to be called in javascript, however this is how we are using it:

return helper.Button(name, buttonText, HtmlButtonType.Butt开发者_C百科on,
    string.Format("window.location='{0}'", url));

which obviously is now broken.

Is there any way to bypass this encoding? I can see hacking it by changing the return type of the method to string, and doing:

return string.Format(helper.Button(name, buttonText, HtmlButtonType.Button,
    "window.location={0}").ToString(), "'" + url + "'");

but this is more or less a hack, and not elegant.


Having the ' should work. Even though the document stream contains the encoded value the browser unencodes it when it builds the DOM. You can use a DOM inspection tool to see yourself.

If I put the following on a page I see the alert box just fine upon click:

<input type="button" onclick="alert(&#39;Hello&#39;);" />


You can use the URI class to convert html codes into regular text

Uri.UnescapeDataString(string);
0

精彩评论

暂无评论...
验证码 换一张
取 消