2013-06-18 191 views
1

我有此方法調用2個其他方法,但執行該代碼時出錯。具有相同名稱的控制器方法

public ActionResult CreateOrder(string action, string type) 
    { 
     /*Some Code*/ 
     if(MyObject.isOk){ 
      return RedirectToAction("EditOrder", new { code = ErrorCode, libelle = Description }); 

     }else{ 
      return RedirectToAction("EditOrder", new { nordre = NoOrdre }); 
     } 
    } 

public ActionResult EditOrder(string nordre) 
    { 

    } 

[ActionName("EditOrder")] 
public ActionResult EditOrderError(string code, string libelle) 
{ 

    } 

,但我得到了404,因爲腳本試圖找到「EditOrderError」視圖。

+1

如何從View中調用此方法? – bayramucuncu

回答

2

ASP.NET MVC不允許你overload controller actions除非他們handle different HTTP verbs

假設你正在使用C#4,一個可能的解決方法,雖然不是一個漂亮的一個,是在單一控制器操作使用可選參數:

public ActionResult EditOrder(string nordre = null, string code = null, string libelle = null) 
{ 
    if (nordre != null) 
    { 
     // ... 
    } 
    else if (code != null && libelle != null) 
    { 
     // ... 
    } 
    else 
    { 
     return new EmptyResult(); 
    } 
} 
0

使用相同的HTTP方法,你可以不超載的控制器操作/動詞(GET/POST/etc)

如果我需要控制器操作以使.NET不允許在標識符中允許使用字符,我將只使用ActionNameAttribute。就像使用破折號(/ controller/create-user)一樣。 Like this one.

相關問題