Frage

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 },
        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.

War es hilfreich?

Lösung

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');

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top