简体   繁体   English

ASP.NET MVC。 将模型中的字符串数据插入javascript。 最佳实践

[英]asp.net mvc. Inserting string data from model into javascript. best-practice

I have to populate javascript code in html layout (hidden fields, method params) with string data from model. 我必须用模型中的字符串数据填充html布局(隐藏字段,方法参数)中的javascript代码。

Html.Encode is not appropriate for my task because it encodes ' symbol, bypass : (that ruines object attributes declaration) and so on. Html.Encode不适合我的任务,因为它会编码'符号,bypass :(破坏对象属性声明)等。

I wrote static helper class that is used from View like this: 我编写了从View中使用的静态帮助器类,如下所示:

  alert('<%=ViewHelper.MakeJavaScriptSafe(Model.Message)%>');

I hope there is asp.net in-built function I don't know about for this task. 我希望有不知道此任务的asp.net内置函数。 Does it exist really? 它真的存在吗?

Thank you in advance. 先感谢您。

我相信您正在寻找的是一个反跨站点脚本库, 就像这个一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC。 检查用户是否已获得JavaScript授权 - ASP.NET MVC. Check if user is authorized from JavaScript 使用ajax post将数据从JS传输到asp.net mvc中Controller中的函数。 但观点没有改变 - Using ajax post to transfer data from JS to a function in Controller in asp.net mvc. But the view is not changed 使用Asp.Net MVC在javascript中删除硬编码字符串的最佳方法 - Best way to remove hardcoded string in javascript with Asp.Net MVC 我想在Asp.Net MVC中使用Ajax显示表数据。 这种Ajax语法有什么问题? - I want to show table data using Ajax in Asp.Net MVC. What is wrong with this Ajax syntax? 在ASP.NET MVC应用程序中包含javascript运行时相关代码的最佳实践 - Best practice on where to include javascript runtime dependent code in ASP.NET MVC apps 从ASP.NET MVC中的JavaScript访问嵌套模型 - Accessing nested Model from javascript in asp.net mvc 如何将模型从partialView传递到asp.net mvc中的javascript - How to pass model from partialView to javascript in asp.net mvc 如何使用来自 model 的数据更新 Javascript 中的 div 标签,用于提交表单 asp.net mvc - How to update div tag in Javascript with data from model for onsubmit form asp.net mvc 将javascript转换为asp.net MVC模型 - var javascript to asp.net MVC model ASP.Net MVC模型绑定到javascript - ASP.Net MVC model binding to javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM