2

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())
)
2
  • replace ToString() to ToHtmlString(). Commented Apr 26, 2015 at 13:34
  • I've tried that before, the result is the same. Commented Apr 27, 2015 at 2:38

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Chintana, now I get the Expected ';' error. This is win: var win = "<div id="f7be8297-92a2-4aef-936b-4b531de497b4"></div><script> jQuery(function(){jQuery("#f7be8297-92a2-4aef-936b-4b531de497b4").kendoWindow({"modal":false,"iframe":false,"draggable":true,"scrollable":true,"pinned":false,"title":null,"resizable":true,"content":null,"actions":["Close","Minimize","Refresh"]});});</script>";
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.
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
In that case, you will have to use ajax to call the partial view
|
0

Well, this line works fine:

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

And the win action will return the partial view.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.