H. Senkaya H. Senkaya - 1 month ago 10
ASP.NET (C#) Question

Controller delivers html as string to jquery to open content in new window

I have the following html button which has the onclick even handler

OpenHTML
:

HTML:

<button type="button" class="btn btn-primary" onclick="OpenHTML('@element.MSNAME')">
Open HTML
</button>


The functions calls a Controller (Get-Method) which delivers a string of html.

Javscript Code:

<script>
function OpenHTML(mappingID) {
var w = window.open();
var html = $.get("/Home/OpenHTMLnew?appID=" + mappingID, function (data) { });

$(w.document.body).html(html);
}
</script>


C#-Controller:

[HttpGet]
public ActionResult OpenHTMLnew(string appID)
{
string html = "<p>test</p>";

return Content(html, "text/html");
}


So far. If I click on the button jQuery opens a new Window as I wished to do. But there is no Content inside the new Window. Can someone help me to deliver
string html = "..."
to new window which jQuery opens.

Thanks in advance!

Answer

Change your script to like this:

<script>
function OpenHTML(mappingID) {
    var w = window.open();
    var html = $.get("/Home/OpenHTMLnew?appID=" + mappingID, function (data) { 
$(w.document.body).html(data);
});


}
</script>

http://prntscr.com/d2zpze
As you can see in image, when you console.log html in function it is returning function not the content.

Comments