未结束的字符串常量中@ Html.Partial从JavaScript对象 [英] Unterminated string constant during @Html.Partial from javascript object

查看:111
本文介绍了未结束的字符串常量中@ Html.Partial从JavaScript对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我在一个页面应用程序的工作,我不希望隐藏容器div元素;而不是我想的HTML内容加载到我的JavaScript对象。

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.

但我得到的错误:

未结束的字符串常量。

我已经试过各种方法,没有任何成功。任何帮助,为什么我得到这个错误将是非常美联社preciated。

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

推荐答案

筛选

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

该ToHtmlString()方法HTML EN codeS你的HTML这样的报价不会混淆你的JavaScript。

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.

更新

_mypartialview

_mypartialview

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

code

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

这输出以下

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

这篇关于未结束的字符串常量中@ Html.Partial从JavaScript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆