【问题标题】:Encompassing object attributes with HTML and return in JSON用 HTML 包含对象属性并以 JSON 格式返回
【发布时间】:2012-03-20 12:07:37
【问题描述】:

目前,我已经编写了以下json搜索方法。

[HttpPost]
    public JsonResult Search(string videoTitle)
    {
        var auth = new Authentication() { Email = "abc@smu.abc", Password = "abc" };
        var videoList = server.Search(auth, videoTitle);
        String html = "";
        foreach(var item in videoList){
            var video = (Video)item;
            html += "<b>"+video.Title+"</b>";
        }

        return Json(html, JsonRequestBehavior.AllowGet);
    }

在屏幕上,它返回这个。

"\u003cb\u003eAge of Conan\u003c/b\u003e"

我该怎么办?我想这样做的原因是我可以使用 CSS 来设置标签样式,这样当项目从搜索输入中下拉时,它看起来更美观。

谢谢

【问题讨论】:

    标签: html json asp.net-mvc-3 jsonresult


    【解决方案1】:

    如果你想返回纯 HTML,你不应该返回 JSON,你应该使用 ContentResult:

    [HttpPost]
    public ContentResult Search(string videoTitle)
    {
        var auth = new Authentication() { Email = "smu@smu.com", Password = "test" };
        var videoList = server.Search(auth, videoTitle);
        String html = "";
    
        foreach(var item in videoList)
        {
            var video = (Video)item;
            html += "<b>"+video.Title+"</b>";
        }
    
        return Content(html, "text/html");
    }
    

    您可以使用标准的 jQuery.get() 请求并直接插入到 DOM 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      相关资源
      最近更新 更多