我的 Controller 是...
public ActionResult CreateEmailStep5()
{
MyViewModel mvm = new MyViewModel();
mvm.myStringProperty = @"$(document).ready(function ($) {
$('#Help').MyTour(
{
tourType: 'step',
overlayOpacity: 0.5,
.......
.......
});
});"
return View(mvm);
}
我正在尝试使用我的 .cshtml 是什么样的......
@model MyProj.ViewModels.MyViewModel
<script type="text/javascript">
Model.myStringProperty;
</script>
我尝试了一些东西,比如 Response.Write、HtmlDecode 和 HtmlEncode... 发生的事情是...我的单引号正在转换为其 ascii "' "
当我尝试时...
Response.Write(@HttpUtility.HtmlDecode(Model.myStringProperty).ToString());
而不是
Model.myStringProperty;
在我的.cshtml中,当我添加了所有必需的链接时,它给出的错误$未定义
最佳答案
您想要将 razor 变量转换为 html 'type'
尝试使用
@Html.Raw(Model.myStringProperty)
关于c# - 从 Controller 动态返回 javascript 函数并将其传递给 cshtml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16397793/