【问题标题】:Render HTML from JSON object returned from controller to jquery ajax call从控制器返回的 JSON 对象渲染 HTML 到 jquery ajax 调用
【发布时间】:2013-05-23 10:05:18
【问题描述】:

在 ASP.NET MVC 4 中,如何在加载时呈现 HTML。我将一些以这种形式编码的 html 字符串保存在我的 sql server 数据库中,类型为 nvarchar(max)。

<li><li><img id="fbPic" src="http://graph.facebook.com/567818188/picture" style="display: inline; padding-right: 10px;"><b>Bernice Zerafa</b></li><pre class="word-wrap" style="margin-left: 10%;">hellooo</pre></li>

*编辑:*请注意,上面的字符串是 html 未正确编码的,因此它实际上显示如下:

<li><li><img id="fbPic" src="http://graph.facebook.com/567818188/picture" style="display: inline; padding-right: 10px;"><b>Bernice Zerafa</b></li><pre class="word-wrap" style="margin-left: 10%;">helloooo </pre></li>

现在在加载页面时,我将获得这些 html 字符串的列表,这些字符串都是带有要附加到无序列表标签的各种子节点的列表项。列表返回正常。但它只是在页面上按原样显示,即显示实际字符串并且没有呈现 html。

这是我的控制器动作:

public ActionResult LoadPosts(int groupId)
{
     var postedText = new List<string>();
     IEnumerable<Post> posts;

     using (CodeShareEntities conn = new CodeShareEntities())
     {
          posts = conn.Posts.Where(g => g.GroupId == groupId); 
          foreach (var post in posts)
          {
               postedText.Add(post.PostData);
          }
     }

     return Json(new { PostedText = postedText });
}

这是我在加载页面时调用的 jQuery Ajax。 #posts 在我的 html 中是一个空的 &lt;ul&gt;

 jQuery.ajax({
     type: 'POST', 
     url: '/Groups/LoadPosts',
     data: { groupId: grpid },
     success: function (postData) {
          $.each(postData, function (index, postText) {
                 **postText = htmlUnencode(postText);**
                 $("#posts")[0].innerHTML = postText;

                //// what can I do here instead of innerHTML to be 
                //// able to view the html correctly ? 
                //// append() and insert() methods in JQuery have the same result
                //// I found something called @Html.Raw(Json.Encode()) maybe
                //// this is relevant here ? How can I use this correctly ?

          });

          $('#posts').css('background', 'white');
     },
     traditional: true
  });

任何帮助将不胜感激!

【问题讨论】:

    标签: c# html jquery asp.net-mvc-4 razor


    【解决方案1】:

    您的 html 似乎是双重编码的。试试这个

         $.each(postData, function (index, postText) {
                 **postText = htmlUnencode(postText);**
                 $("#posts").append($($("<div/>").html($("<div/>").html(test).text()).text()));
          });
    

    这是一个Fiddle 示例。

    【讨论】:

    • 成功了!我找了好久!以为我必须以其他方式渲染它!谢谢!!
    • hmm.. 但是看看是否有更好的方法来做到这一点。此外,它似乎是双重编码的 ex:- 而不是 &amp;amp;lt; 我猜它应该是 &amp;lt;。顺便说一句,欢迎你... :)
    • 请问您在这段代码中到底在做什么?嗯,我也会调查的!
    • @Bernice 试试这个。在发回字符串之前,在您的服务器中执行此操作htmlstring = WebUtility.HtmlDecode(htmlstring) 并发回并在 ajax 调用中执行一次。 $("#posts").append($("&lt;div/&gt;").html(test).text())
    • 哦,我明白了!我发现我在做什么!在测试时,我还在控制器中做了 WebUtility.HtmlEncode,所以我也从 js 端对它进行了编码!谢谢你的解释!
    【解决方案2】:

    只要看看你的

    &amp;lt;li&amp;gt;&amp;lt;li&amp;gt;&amp;lt;img id=&amp;quot;fbPic&amp;quot; src=&amp;quot;http://graph.facebook.com/567818188/picture&amp;quot; style=&amp;quot;display: inline; padding-right: 10px;&amp;quot;&amp;gt;&amp;lt;b&amp;gt;Bernice Zerafa&amp;lt;/b&amp;gt;&amp;lt;/li&amp;gt;&amp;lt;pre class=&amp;quot;word-wrap&amp;quot; style=&amp;quot;margin-left: 10%;&amp;quot;&amp;gt;hellooo&amp;lt;/pre&amp;gt;&amp;lt;/li&amp;gt;
    

    我怀疑它会正确呈现它似乎不是一个有效的 html 并且你不断地附加所以你可能会使用类似的东西

      $("#posts").append(postText);
    

    【讨论】:

    • 不,它是有效的 html,实际上浏览器将其显示为嵌套在
    • 中的普通标签。尝试追加,但文本仍然显示在 ul 而不是实际节点中
  • 也许你没有正确粘贴它,但它看起来对我无效jsfiddle.net/hdvLf
  • 你是对的。我忘记将未编码包含回正确的标签。请参阅编辑。我包括了它是如何准确显示的
  • jsfiddle.net/hdvLf/1 所以你想看到这个总线而不是你看到的只是纯 html 字符串
  • 没错!这就是我想要的结果,但我看到的是实际的 html 字符串
  • 猜你喜欢
    相关资源
    最近更新 更多
    热门标签