I was going to use declarative HTML helpers, but then found out that they have not been implemented in a release of MVC 3.
I'm trying to get old HTML helpers to work with the following code:
private static String GenerateSingleOptionHTML(Question q)
{
String ret = "";
for(int i = 0; i < 3; i++)
{
ret += String.Format("<li><input type=\"radio\" id=\"Q" + i +"\" value=\"" + i + "\" name=\"Q" + i +"\" />" + q.Body + "</li>");
}
return ret;
}
Ignore the html and tag as they work fine. What I get in my view, is: " <li><input type="radio" id="Q0" value="0" name="Q0" />Body Question 1</li><li><input type="radio" id="Q1" value="1" name="Q1" />Body Question 1</li><li><input type="radio" id="Q2" value="2" name="Q2" />Body Question 1</li> " rather than formatted HTML.
MvcHtmlString 对象将被视为在渲染期间已编码(我假设您使用的是 <%: %> 语法而不是 <%= %> 将 HTML 注入到页面中)。
return MvcHtmlString.Create(ret);
You need to return an instance of MvcHtmlString. Your output string is getting encoded.
The MvcHtmlString object will be treated as already encoded during rendering (I assume you're using the <%: %> syntax instead of <%= %> to inject the HTML into the page).
发布评论
评论(2)
David Neale 是对的,但在 ASP.NET MVC 3 中,您实际上应该返回
HtmlString
的实例,而不是MvcHtmlString
(不过,两者都可以工作):David Neale is right, but in ASP.NET MVC 3 you should actually return an instance of
HtmlString
, notMvcHtmlString
(both will work, though):您需要返回 MvcHtmlString 的实例。您的输出字符串正在被编码。
MvcHtmlString
对象将被视为在渲染期间已编码(我假设您使用的是<%: %>
语法而不是<%= %>
将 HTML 注入到页面中)。You need to return an instance of MvcHtmlString. Your output string is getting encoded.
The
MvcHtmlString
object will be treated as already encoded during rendering (I assume you're using the<%: %>
syntax instead of<%= %>
to inject the HTML into the page).