Unterminated string constant during @Html.Partial from javascript object

安稳与你 提交于 2020-01-06 19:32:47

问题


var html = '@Html.Partial("_mypartialview")';
$("#container").html(html);

I am working in a single page application and I don't want to hide the container div element; instead I want to load the html content into my javascript object.

But I get the error:

Unterminated string constant.

I have tried various methods without any success. Any help as to why I get this error would be really appreciated.


回答1:


Like this

var html = "@Html.Partial("_mypartialview").ToHtmlString().Replace("\n", "<br/>").Replace(" ", "")";

The ToHtmlString() method HTML encodes your html so the quotes won't confuse your JavaScript.

The two replace methods will remove new lines and whitespace so the content is all on one line.

Update

_mypartialview

 <div>
     <span data-bind="text:playerInfo.Name"> </span> 
</div> 

Code

 var html = "@Html.Partial("_mypartialview").ToString().Replace(Environment.NewLine, "").Replace(" ", "")"

This outputs the following

var html = "&lt;div&gt;&lt;spandata-bind=&quot;text:playerInfo.Name&quot;&gt;&lt;/span&gt;&lt;/div&gt;"


来源:https://stackoverflow.com/questions/28761693/unterminated-string-constant-during-html-partial-from-javascript-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!