开发者

How to Overload ActionResult in Asp.Net MVC2

I have problem wi开发者_运维问答th 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.


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) { /* ... */ }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜