2010-06-25 24 views
2

我有問題的ActionResult方法MVC2如何重載的ActionResult在Asp.Net MVC2

超載我有3種方法

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); 
} 

我怎麼能在超載MVC2 Asp.Net方法? 謝謝。

回答

2

簡而言之,你不能僅僅通過變量來重載方法。

Discussion on Stack

如果你必須有相同的方法名,你需要創建一個ActionFilter屬性,並以此作爲你的過載。

段從上面的討論:

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

[RequireRequestValue("someString")] 
public ActionResult MyMethod(string someString) { /* ... */ } 
+3

在命名空間爲[RequireRequestValue()] – 2010-06-26 00:43:59

+0

你必須創建操作篩選自己並調用它任何你想要的。重新閱讀我發佈的問題,看看他如何解決他的困境。 – Tommy 2010-06-26 02:49:01

+0

我的錯。 現在工作。謝謝。 – 2010-06-26 07:55:15