1

I'm trying to render a partial view within my javascript code. The partial view itself contains JS codes as well, it's a kendo window.

This is my code, with changes suggested in this post:

var win = "@Html.Partial("_GenericWindow").ToString().Replace(Environment.NewLine, "").Replace(" ", "")";

$(".body-content").append(win);

But this code renders string in my page:

<divid="7067ea1a-0425-44a6-99d3-aefb37f088ed"></div><script> jQuery(function(){jQuery("#7067ea1a-0425-44a6-99d3-aefb37f088ed").kendoWindow({"modal":false,"iframe":false,"draggable":true,"scrollable":true,"pinned":false,"title":null,"resizable":true,"content":null,"actions":["Close","Minimize","Refresh"]});});</script>

And finally, this is my partial view:

@{
    var winId = Guid.NewGuid().ToString();
}

@(Html.Kendo().Window()
    .Name(winId)
    .Draggable()
    .Resizable()
    .Actions(actions => actions.Close().Minimize().Refresh())
)
Community
  • 1
  • 1
Akbari
  • 2,369
  • 7
  • 45
  • 85

2 Answers2

2

The issue is MVC is preventing you from injecting raw HTML to the response stream.

You can modify your code by introducing @Html.Raw() to tell MVC that you want it to inject the raw HTML

ex..

var win = "@Html.Raw(@Html.Partial("_GenericWindow").ToString().Replace(Environment.NewLine, "").Replace(" ", ""))";

$(".body-content").append(win);
Chintana Meegamarachchi
  • 1,798
  • 1
  • 12
  • 10
  • Thanks Chintana, now I get the `Expected ';'` error. This is win: `var win = "
    ";`
    – Akbari Apr 27 '15 at 02:41
  • This is my current line: `var win = "@Html.Raw(@Html.Partial("_GenericWindow").ToHtmlString().Replace(Environment.NewLine, ""))";` Also, I'll be thankful if you could guide me about getting the window's handle too. – Akbari Apr 27 '15 at 02:43
  • This might be due to something going wrong at the server side code. You can test this by replacing all code in _GenericWindow with a simple html message and see whether renders – Chintana Meegamarachchi Apr 27 '15 at 02:49
  • The problem is I'll have JS code inside the returned value. I just want to have [multiple windows](http://stackoverflow.com/questions/29817307/opening-multiple-windows-with-kendo-menu/29837117#29837117), any better way? – Akbari Apr 27 '15 at 02:58
  • 1
    In that case, you will have to use ajax to call the partial view – Chintana Meegamarachchi Apr 27 '15 at 03:01
  • Can you give me an example? – Akbari Apr 27 '15 at 03:03
  • 1
    http://stackoverflow.com/questions/1570127/render-partial-view-using-jquery-in-asp-net-mvc – Chintana Meegamarachchi Apr 27 '15 at 03:05
  • Yes, I can have simple HTML. JS code is causing the issue. – Akbari Apr 27 '15 at 03:06
0

Well, this line works fine:

$('.body-content').load('/Home/win?window='.concat(text));

And the win action will return the partial view.

Akbari
  • 2,369
  • 7
  • 45
  • 85