2

I have problem with ActionResult Method overloading in MVC2

I have 3 methods

public ActionResult MyMethod()
{  
   var data = ........
   //some unfiltered data from db
   return view(data);
}

public ActionResult MyMethod(string name)
{
   var data = .......
              Where xxx.StartsWith(name)
   //some filtered data by name
   return View(data);
}

public ActionResult MyMethod(int age)
{
   var data = .......
              Where xxx.Equals(age)
   //some filtered data by age
   return View(data);
}

How can i overload methods in Asp.Net MVC2? Thanks.

Peter M.
  • 1,028
  • 2
  • 10
  • 27

1 Answers1

2

Short answer, you can't overload methods solely by variables.

Discussion on Stack

If you must have the same method names, you will need to create an actionfilter attribute and use that as your overload.

Snippet from above discussion:

[RequireRequestValue("someInt")]
public ActionResult MyMethod(int someInt) { /* ... */ }

[RequireRequestValue("someString")]
public ActionResult MyMethod(string someString) { /* ... */ }
Community
  • 1
  • 1
Tommy
  • 39,592
  • 10
  • 90
  • 121
  • 3
    in which namespace is [RequireRequestValue()] – Peter M. Jun 26 '10 at 00:43
  • You have to create the action filter yourself and call it whatever you want. Re-read the question that I posted to and look @ how he solved his dilemma. – Tommy Jun 26 '10 at 02:49
  • And can you vrite me filter example if i wana use Mymethod(string a, string b, string c) ?thanks – Peter M. Jun 26 '10 at 09:28
  • 1
    What have you tried so far to overload a method with mulitple parameters? I will help you debug your code, but not write it for you... – Tommy Jun 26 '10 at 14:08