Razor编码引用的html属性

时间:2013-05-21 19:35:08

标签: asp.net-mvc razor

我想使用此结构有条件地向客户端呈现适当的HTML:

<input type="button" value="Foo" @(string.IsNullOrEmpty(Model.Identifier) ? string.Format("title={0} disabled=disabled", "Lorem ipsum") : "onclick=window.open('http://www.google.com'); return false;") />

这是我得到的输出:

<input type="button" value="Foo" title=&quot;Lorem ipsum&quot; disabled=disabled />

我尝试了很多Html.Raw()结构,但似乎没有任何帮助。如何使用引号而不是html实体正确输出未编码的HTML?

2 个答案:

答案 0 :(得分:4)

试试这个。刚尝试过,它对我有用。区别在于单数引号和围绕整个事物的Html.Raw

<input type="button" value="Foo" @Html.Raw(string.IsNullOrEmpty(Model.Identifier) ? string.Format("title='{0}' disabled='disabled'", "Lorem ipsum") : "onclick='window.open(\"http://www.google.com\"); return false;'") />

答案 1 :(得分:-2)

我认为添加单引号就足够了

..."title='{0}' disabled='disabled'"...
相关问题