简体   繁体   English

如何从视图将字符串作为参数传递给javascript函数?

[英]How to pass a string as a parameter into a javascript function from a View?

I've just started to work with ASP.NET MVC projects, I have gotten past the basics, but I came across with a problem recently, I can't pass a string as a id from a View. 我刚刚开始使用ASP.NET MVC项目,已经了解了基础知识,但是最近遇到了一个问题,我无法从View传递字符串作为id。

So far, I've been passing the id to a javascript function like this: 到目前为止,我一直在将ID传递给这样的javascript函数:

<script>app.init( <%: ViewContext.RouteData.Values["id"].ToString() %>)</script>

This calls the following javascript function: 这将调用以下javascript函数:

var app = {};
(function (app) {
    var _id;
    app.init = function (id) {
        _id = id;
    }
    app.id = function () {
        return _id;
    }
})(app);

I'm doing this because I need to work with the id to perform operations like submits and ajax calls in javascript (I prefer working with javascript). 我这样做是因为我需要使用id来执行诸如javascript中的Submit和ajax调用之类的操作(我更喜欢使用javascript)。

So far this has worked with int values, but when I tried to pass a string it fails. 到目前为止,这已经可以使用int值了,但是当我尝试传递字符串时,它失败了。 For example if I try to access to this url localhost:65097/Device/Edit/D2C02 it shows the following error: 例如,如果我尝试访问此URL localhost:65097 / Device / Edit / D2C02,则会显示以下错误:

SCRIPT5009: 'D2C02' is undefined

When I check the generated html code, the call to the function looked like this: 当我检查生成的html代码时,对该函数的调用如下所示:

<script>app.init(D2C02)</script>

Shouldn't the id be passed as 'D2C02' like a string, instead of just D2C02 like a variable? 难道不应该像字符串一样将ID作为'D2C02'传递,而不是像变量一样作为D2C02传递吗? Because that's what I'm understading the browser thinks the id D2C02 represents. 因为这就是我要理解的,浏览器认为ID D2C02代表。 How can I make sure the id value is passed on to the javascript function as a string? 如何确定id值作为字符串传递给javascript函数?

ToString() just converts something to a string - but that's a C# string, not a JS string. ToString()只是将某些内容转换为字符串-但这是C#字符串,而不是JS字符串。 So the easiest solution is to add quotes assuming the value can never contain quotes, too: 因此,最简单的解决方案是在假定值也不能包含引号的情况下添加引号:

<script>app.init("<%: ViewContext.RouteData.Values["id"].ToString() %>")</script>

If it may contain quotes you can either perform a simple replacement to backslash-escape them or run the string through a JSON serializer that takes care of turning it into a valid JavaScript string. 如果其中可能包含引号,则可以执行简单的替换以将其反斜杠转义,或者通过JSON序列化程序运行该字符串,以将其转换为有效的JavaScript字符串。

Try 尝试

app.init("<%......%>"); app.init(“ <%......%>”);

Otherwise you are just posting in a variable name and not string. 否则,您只是以变量名而不是字符串发布。 The ToString() method in c# doesn't wrap the vale in quotes C#中的ToString()方法不会将vale括在引号中

The approach I would use is to do something like this, to make things a bit more semantic -- this is overly simplified to demonstrate: 我将使用的方法是执行类似的操作,使事情更具语义性-对此进行了过度简化以演示:

<body>
    <input id="myId" type="hidden" value="<%: ViewContext.RouteData.Values["id"].ToString() %>" />

...
<script language="text/javascript">
    function getId() {
        return document.getElementById("myId").value;
    }
</script>
</body>

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

相关问题 将字符串从控制器传递到局部视图作为javascript函数参数 - Pass a string from the controller to a partial view as javascript function parameter 如何将多个参数从javascript函数传递到视图内的控制器? - How to pass multiple parameter from javascript function to controller inside a View? 如何将字符串值从html作为函数参数传递给javascript? - how to pass string value to javascript as function parameter from html? 如何将参数从javascript传递到Ajax函数 - How to pass parameter from javascript to Ajax function 如何从Javascript函数将参数传递给XSLT - How to pass parameter to XSLT from Javascript function Javascript:如何将带有字符串参数的函数作为参数传递给另一个函数 - Javascript: How to pass a function with string parameters as a parameter to another function 如何在JavaScript函数中传递两个视图模型参数 - How to pass two view model parameter in javascript function 如何将JSON字符串从Controller动作传递到View并作为JQuery函数的参数加载 - How to pass JSON string from Controller action to View and load as parameter for JQuery function 从服务器端将多个字符串值作为参数传递给javascript函数 - Pass multiple string values as parameter from server side to javascript function 如何在javascript参数函数中传递php post方法字符串? - how to pass php post method string in javascript parameter function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM