如何在 ASP.NET Core 6 MVC 控制器中动态创建 jquery 函数名称?

问题描述 投票:0回答:1

在我的 ASP.NET Core 6 MVC 应用程序中,我尝试动态创建 jQuery 函数名称。

我在控制器中的代码是这样的:

string int= 1 // sid is a variable

string fn = "function test_+{sid}() {
             var params = 'top = 0, left = 0';params +='width =' + screen.width + ', height =' + screen.height + ',minimizable = no,statusbar = no,toolbar = no,location = no,directories = no,menubar = no,resizable = no,copyhistory = no';
             params += ', scrollbars = yes, status = no, fullscreen = yes';
             var url =document.URL;
             newwin = window.open('/user/instruction?p1=" + sid + @"&p2=" + dr["DifficultyLevel"].ToString() + @"&s1=" + 'd' + @"&p3=" + dr["DifficultyLevel"].ToString() + @", _blank, params');"

这里

sid
在循环内运行。
sid
需要动态更改,并将添加函数名称
test_
作为后修复。我已经尝试过
{}
但它不起作用。

jquery asp.net-core-mvc asp.net-core-6.0
1个回答
0
投票

通过在每行末尾添加简单的 + 即可实现这一点。请找到下面的示例代码,可以用作任何控制器中的操作方法进行测试。

public string Index()
    {
        List<string> functionNames = new List<string>();
        for (int i = 0; i < 10; i++)
        {
            var name = Guid.NewGuid().ToString();
            var fn = @"function test_" + name + "}(){" +   //Add + at the end of the each line of js function 
                        "console.log('" + DateTime.Now.ToString() + "');" +
                       "} ";
            functionNames.Add(fn);
        }

        return string.Join("----", functionNames);

    }
© www.soinside.com 2019 - 2024. All rights reserved.