简体   繁体   English

asp.net razor javascript变量中的特殊字符问题

[英]asp.net razor special character issue in javascript variable

I'm using razor in webmatrix and I have a problem with the string special character. 我在webmatrix中使用razor,我遇到字符串特殊字符的问题。 I want to put a string into a varibale in the javascript but when I try to use the tag @ it shows a problem this is the code : 我想在javascript中将一个字符串放入一个varibale但是当我尝试使用标签@时它显示一个问题,这就是代码:

<script>
 $Edit.click(function() {
        var userid = $(this).closest('td').prev('td').text();
     var stringDiv = "@{ var selectQueryString4 =  SELECT * FROM DemandeConge where UserId = '\"+ trim(userid) + \"'\"+\";<h3>modifier la demande de congé de \" + userid +  \"</h3>foreach(var row in db.Query(selectQueryString4)){<form action='responsable.cshtml' method=\"post\"><label for=\"txtDebut\">Date début :</label><input type=\"text\" id=\"txtDebut\" value=\"+ @row.DateDebutDemande +\"/></br><label for=\"txtFin\">Date de fin :</label><input type=\"text\" id=\"txtFin\" value=\"+ @row.DateFinDemande +\"/></br> <label for=\"txtTypeCong\">Type de congé :</label><input type=\"text\" id=\"txtTypeCong\" value=\"+ @row.TypeConge +\"/><input type=\"hidden\" name=\"UserId\" value=\"+ @row.UserId +\" /><input type=\"hidden\" name=\"type\" value=\"edite\" /><input type=\"submit\" value=\"Oui\"  /> <input type=\"button\" value=\"No\" onclick=\"$('#edite').slideUp(); document.getElementById('edite').innerHTML = ' '\" /> </form>}}}");


    document.getElementById('edite').innerHTML = stringDiv ;
        $('#edite').slideDown(1000);

        )
</script>

So I want to know if there is any way to solve this problem. 所以我想知道是否有办法解决这个问题。

使用double @ like @@而不是@@

Based on your error , you must be using ' ' (simple quotes ) somewhere and it get compiled by the razor engine. 根据您的错误,您必须在某处使用''(简单引号)并由剃刀引擎编译。 try to change all your ' in \\" in your "" strings. 尝试在“”字符串中更改所有“in”。

try for yourself , if you declare a string like this 'aa' you will get the same error in Csharp ,because you have to use "aa" to declare a string literal. 试试你自己,如果你声明一个类似'aa'的字符串,你会在Csharp中得到同样的错误,因为你必须使用“aa”来声明一个字符串文字。 '' are for character literals. ''是为了字符文字。

anyway in my opinion you should not mix js and C#. 无论如何,在我看来你不应该混合使用j和C#。 if you need to pass variables from C# to Js use either ajax ,or another solution like hidden input fields. 如果需要将变量从C#传递给Js,请使用ajax或其他解决方案,如隐藏输入字段。 or create templates like that : 或者创建这样的模板:

<script type='text/template' id='mytemplate'>
<div>@myVar</div>
</script>

and get the content of the template with 并获取模板的内容

$templateContent= $("#mytemplate").html()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM