呼吁JsonResult @ Html.Action改变父模板我响应类型 [英] calling @Html.Action for JsonResult changes my response type in parent template

查看:124
本文介绍了呼吁JsonResult @ Html.Action改变父模板我响应类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下控制器:

 公共类的HelloController
{
    公众的ActionResult指数()
    {
        返回查看()
    }    公众的ActionResult你好()
    {
        返回JSON(新{=问候你好,世界!},JsonRequestBehavior.AllowGet);
    }
}

然后,在 Index.cshtml

  ... HTML东西
<脚本类型=文/ JavaScript的>
    警报(@ Html.Action(你好));
< / SCRIPT>

什么我发现是,在我的浏览器去这个网址时,响应内容类型为应用程序/ JSON的;字符集= UTF-8 这会导致浏览器呈现HTML作为字符串而不是作为...网页。

什么是解决这个问题的最好方法?


解决方案

之所以这是所有 Html.Action 调用是直接执行。是这样的:


  1. 索引被称为

  2. 执行中查看结果

  3. 您好被执行的动作,设定的ContextType

  4. 索引视图结果返回

  5. 浏览器显示的页面

您有两个选择:


  1. 打破了产生逻辑世界,你好!成常规的C#类,并在索引控制器动作直接调用它

  2. 加载通过AJAX的Hello操作,然后显示警报

选项1

 公共类的HelloController
{
    YourBusiness _yb;    公共HelloController中(YourBusiness YB)
    {
        _yb = YB;
    }
    公众的ActionResult指数()
    {
        返回查看(yb.GenerateHello())
    }    //用于一切,但指数
    公众的ActionResult你好()
    {
        返回JSON(新{问候= yb.GenerateHello()},JsonRequestBehavior.AllowGet);
    }
}公共类YourBusiness
{
    公共字符串GenerateHello()
    {
        返回你好世界国际!;
    }
}

选项2

 <脚本类型=文/ JavaScript的>
    $获得(@ Url.Action(你好)',函数(响应){
        警报(response.greeting);
    }
< / SCRIPT>

边注

Internet Explorer中相当积极,当涉及到缓存。该JSON响应将被改变。因此,我建议你也指定没有缓存为JSON动作:

  [的OutputCache(持续时间= 0,NoStore = TRUE)]
公众的ActionResult你好()
{
    返回JSON(新{=问候你好,世界!},JsonRequestBehavior.AllowGet);
}

I've got the following controller:

public class HelloController
{
    public ActionResult Index()
    {
        return View()
    }

    public ActionResult Hello()
    {
        return Json(new{ greeting = "hello, world!" }, JsonRequestBehavior.AllowGet);
    }
}

Then, inside Index.cshtml:

...html stuffs
<script type="text/javascript">
    alert("@Html.Action("Hello")");
</script>

What I'm finding is that, when going to this url in my browser, the response content type is application/json; charset=utf-8 which causes the browser to render the html as a string instead of as... a web page.

What's the best way to get around this?

解决方案

The reason to this is that all Html.Action invocations are executed directly. Something like:

  1. Index is called
  2. View result is executed
  3. Hello action is executed, set's ContextType
  4. Index view result is returned
  5. Browser displays the page

You got two options:

  1. Break out the logic which generates "Hello world!" into a regular C# class and invoke it directly in the Index controller action
  2. Load the Hello action through ajax and then display the alert.

Option 1

public class HelloController
{
    YourBusiness _yb;

    public HelloController(YourBusiness yb)
    {
        _yb = yb;
    } 
    public ActionResult Index()
    {
        return View(yb.GenerateHello())
    }

    // used for everything but Index
    public ActionResult Hello()
    {
        return Json(new{ greeting = yb.GenerateHello() }, JsonRequestBehavior.AllowGet);
    }
}

public class YourBusiness
{
    public string GenerateHello()
    {
        return "Hello wolrd!";
    }
}

Option 2

<script type="text/javascript">
    $.get('@Url.Action("Hello")', function(response) {
        alert(response.greeting);
    }
</script>

Side note

Internet Explorer is quite aggressive when it comes to caching. The JSON responses will be changed. I therefore recommend that you also specify no cache for the JSON action:

[OutputCache(Duration = 0, NoStore = true)]
public ActionResult Hello()
{
    return Json(new{ greeting = "hello, world!" }, JsonRequestBehavior.AllowGet);
}

这篇关于呼吁JsonResult @ Html.Action改变父模板我响应类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆