2013-06-25 73 views
2

我收到了一個令人困惑的錯誤,我不太清楚爲什麼。當你有兩個ActionResults時,通常會出現這種錯誤,忘記[HttpPost]。但正如你所看到的,我[HttpPost]那裏,所以有什麼可能導致這個問題?已經使用相同的參數類型定義了名爲'Search'的成員

錯誤:Type 'PersonalWebsite.Controllers.BlogController' already defines a member called 'Search' with the same parameter types Controllers\BlogController.cs

和代碼:

// 
// GET: /Blog/Search 

public virtual ActionResult Search() 
{ 
    return RedirectToAction(MVC.Blog.Index()); 
} 

// 
// POST: /Blog/Search 

[HttpPost] 
[ValidateInput(false)] 
public virtual ActionResult Search(SearchViewModel model) 
{ 
    // irrelevant code snipped 

    return View(model); 
} 

有在該控制器中沒有定義任何其他Search()方法。這很奇怪。

任何想法?

+1

什麼是基類? – haim770

+0

'公共部分類BlogController:Controller' - 通用基類 –

+1

你可以發佈整個控制器嗎?當你遇到你提到的錯誤時, – CodeCaster

回答

2

您的Search方法已在另一個partial中定義。

在這裏看到:https://github.com/Imdsm/PersonalWebsite/blob/master/PersonalWebsite/BlogController.generated.cs

[NonAction] 
[GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode] 
public virtual System.Web.Mvc.ActionResult Search() 
+0

這是有道理的。沒想到在T4MVC中看。只需要再次運行T4MVC。我想我已經安裝了[Chirpy](https://chirpy.codeplex.com/downloads/get/212976)來爲我做這個。謝謝haim770! –

+0

作爲未來搜索者的一個便箋,Chirpy似乎並不是最好的選擇,但我確實在NuGet上找到了一些名爲AutoT4MVC的東西,在這裏找到:https://github.com/bennor/AutoT4MVC –

2

你可以通過你的方法創建別名:

[HttpPost] 
    [ValidateInput(false)] 
    [ActionName("Search")] 
    public virtual ActionResult SearchByPost(SearchViewModel model) 
    { 
     // irrelevant code snipped 

     return View(model); 
    } 
相關問題