Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#

like image 267
Fraz Sundal Avatar asked Jan 19 '12 11:01

Fraz Sundal


1 Answers

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).

like image 155
Sergey Kudriavtsev Avatar answered Nov 14 '22 22:11

Sergey Kudriavtsev