开发者

Set ContentType of new window opened in JavaScript following MVC post

I need to open a new window and set the contents to whatever was determined by a post to my MVC controller when a link is clicked. Here's how I'm currently doing it.

jQuery.ajax({
        type: "POST",
        url: '/controller/mycontroller',
        data: { mydata: data },
      开发者_JS百科  error: function (xhr, status, error) {
        },
        success: function (response) {
            win_detail = window.open('', 'name');
            win_detail.document.write(response);
        }
    });

The controller currently is processing what html should be on the page and putting it in the ViewBag. It's also putting what the Response.ContentType should be in the ViewBag. My mycontroller.cshtml I have something like this.

@{
Response.ContentType = ViewBag.ContentType;
}

<head></head>
<body>
@ViewBag.MyHtml
</body>

This doesn't actually set the ContentType. Does anyone know how I can? I can change my entire structure to whatever accomplishes this.


Absent some other reason, I'd attempt to use a GET request and just open the window to the given URL.

win_detail = window.open('/controller/action?mydata=' + data, 'name');

Depending on your data you may need to serialize it differently.

var form_data = $('form').serialize();
win_detail = window.open( '/controller/action?' + form_data, 'name');


Instead of passing the ContentType to the view via ViewBag, try setting it in the controller before you return.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜