2012-10-11 73 views
0

我有兩種方法:在asp.net mvc中的Actions中含糊不清,如何解決它?

public ActionResult Index(int? id) 
{ 
    return Redirect("/View/" + this.GetMenuItems().First().Id); 
} 

public ActionResult Index(int id, uint? limit) 
{ 

,當我去/查看/ 1 - 我得到這個錯誤

他的行動控制器類型「索引」當前請求「視圖控制器」不明確以下動作的方法之間: System.Web.Mvc.ActionResult指數(System.Nullable 1[System.Int32]) on type SVNViewer.Controllers.ViewController System.Web.Mvc.ActionResult Index(Int32, System.Nullable 1 [System.UInt32])上式SVNViewer.Controllers.ViewController

我需要這兩種方法,但刪除不明確的錯誤,我該怎麼做?

+1

重命名的方式到別的一個:第二個方法首頁 - > MenuItem也許?或者,在第二個參數中設置限制參數(不可爲空)。 – Tommy

+0

@Tommy如果你回答爲回答 – VJAI

+0

@Mark - 我很欣賞,但我沒有足夠的時間來制定一個很好的答案,當我遇到這個,但想扔在我的2美分爲testCoder! – Tommy

回答

2

將您的第二個操作更改爲不具有可爲空的uint。

public ActionResult Index(int id, uint limit) 

Index(int? id)應負責處理,如果限制是空的方法。

2

您可以使用ActionName來解決此問題:Purpose of ActionName

如果都做同樣的事情,你可以做,而不是:

public ActionResult Index(int? id, uint? limit = null) 
{ 
    ... 
} 

使第二個參數可選。

或者有一個[HttpGet]屬性和一個[HttpPost],如果一個響應一個get而另一個響應一個發佈表單。

1

您可以創建一個模型,只有一個動作方法:

public class MyActionModel 
{ 
    public int? Id { get;set; } 
    public int? Limit { get;set; } 
} 

然後在你的控制器:

public ActionResult Index(MyActionModel model) 
{ 
    // Your code here 
    if (model.Id.HasValue() { // do something....etc} 
} 
相關問題