Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用视图变量从视图编码模型方法?_C#_Jquery_Asp.net Mvc_Razor - Fatal编程技术网

C# 如何使用视图变量从视图编码模型方法?

C# 如何使用视图变量从视图编码模型方法?,c#,jquery,asp.net-mvc,razor,C#,Jquery,Asp.net Mvc,Razor,我需要在ajax上调用一个动作方法。它需要一个int参数。 对于Ajax的url,我在模型中使用了两种方法。 CommonClass.CombineUrls(string1,string2)组合2个URL&CommonClass.GetRootPath()以获取应用程序根路径。 这是我的密码: function GetDetails(ContactId) { $.ajax({ async: true, type: "POST

我需要在ajax上调用一个动作方法。它需要一个int参数。 对于Ajax的url,我在模型中使用了两种方法。 CommonClass.CombineUrls(string1,string2)组合2个URL&CommonClass.GetRootPath()以获取应用程序根路径。 这是我的密码:

    function GetDetails(ContactId) {
        $.ajax({
            async: true,
            type: "POST",
            dataType: "json",
            url: "@CommonClass.CombineUrls(CommonClass.GetRootPath(), "Controller1/Method1/" + ContactId)",
            success: function (data) {
                alert("Name: " + data.FirstName);
            }
        });
    }

“联系人ID”对url不可用。正如所说,它将所有内容都视为服务器端代码。那么,有没有办法将服务器端和客户端代码结合起来呢?

你不能像那样在Razor中使用JavaScript变量,你需要做的是类似这样的事情,使用
Url。操作

url: "@Url.Action("Method1", "Controller1")" + "?ContactId=" + ContactId
或者(和更好的():

如果
Url.Action
在您的场景中不可能/不适用,则使用现有功能,但仍使用
数据
属性:

url: "@CommonClass.CombineUrls(CommonClass.GetRootPath(), "Controller1/Method1/")",
data: { ContactId: ContactId }

我正在使用您提供的第三个选项。但是,我希望有一种结合使用JS变量和服务器端代码的方法。这样我就可以避免ajax调用的数据。:)@Rasmitash如果不能在一个方法调用中组合这两个函数,那么必须有一个单独的AJAX调用来传递变量,然后返回函数的结果,然后在第二个AJAX调用中使用该结果。我建议使用
数据
,只需一次调用。
url: "@CommonClass.CombineUrls(CommonClass.GetRootPath(), "Controller1/Method1/")",
data: { ContactId: ContactId }