简体   繁体   中英

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.

I have two functions in c#

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

Now when i call function which has one parameter in jquery like this

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#

ASP.NET MVC generally does not allow action overloads. 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). 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.

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 . If b is null then call func(a) overload, otherwise call func (a,b).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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