简体   繁体   English

如何在Asp.Net MVC 2中通过JQuery调用重载的C#函数

[英]How to call overloaded C# function through JQuery in Asp.Net MVC 2

I am having a problem while calling overloaded C# function through jquery post method. 我通过jquery post方法调用重载的C#函数时遇到问题。

I have two functions in c# 我在c#中有两个函数

string func(string a)
string func(string a, string b)

Now when i call function which has one parameter in jquery like this 现在当我调用jquery中有一个参数的函数时,就像这样

var url = '<%= Url.Action("func", "Team") %>';                
$.post(url, { a: "test" }, function (data) {
});

It gives me this error 它给了我这个错误

The current request for action 'func' on controller type 'TeamController' is 
ambiguous between the following action methods

I want to know how can i call these overloaded functions. 我想知道如何调用这些重载函数。 Im using Asp.Net MVC 2 with C# 我用C#使用Asp.Net MVC 2

ASP.NET MVC generally does not allow action overloads. ASP.NET MVC通常不允许动作重载。 If you want to do different things depending on content you're sending to server then you should consider seprating processing between two functions with different names (different action names and different URLs apparently). 如果你想根据你发送给服务器的内容做不同的事情,那么你应该考虑在两个具有不同名称的功能之间分别处理(显然不同的动作名称和不同的URL)。 If you want to keep the overloading in code then still you should decorate those functions with [ActionName("%some_name_here%")] attributes giving them different names. 如果你想在代码中保持重载,那么你仍然应该用[ActionName("%some_name_here%")]属性来装饰这些函数,为它们赋予不同的名称。

If you want to keep single action name then create a "Front-End" action with two parameters a and b and there in action code check if b==null . 如果要保留单个操作名称,则创建一个带有两个参数ab的“前端”操作,并在操作代码中检查b==null If b is null then call func(a) overload, otherwise call func (a,b). 如果b为null,则调用func(a)重载,否则调用func(a,b)。

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

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