2012-12-31 74 views
1

我正在ASP.NET MVC網站上工作,我是新手。 我有一個很少動作的控制器。我想通過我的網站使用這些操作。 例如重定向到相同的視圖

[HttpPost] 
public ActionResult MyAction(ViewModel model) 
{ 

    if (ModelState.IsValid) 
    { 
     //code is here 
    } 
    return RedirectToAction(); // redirect to same view 

} 

我想重定向到從生成請求,其中相同的視圖。我不確定這是否可行?

+0

只返回查看(),這將是罰款... –

+0

你想要什麼是可能的,但是由於你的問題的存在含糊不清,很難理解你想在什麼環境下工作。我假設你想保留你的代碼[DRY](http://en.wikipedia.org/wiki/Dry),但是你想重用動作邏輯還是查看結果? –

+0

@Erik Philips。我想重用我的視圖結果和行動邏輯 – sanjeev

回答

0

基於您的評論,我想創建一個控制器,看起來像:

public MyController : Controller 
{ 
    private ActionResult SharedMethod(SomeModel model) 
    { 
    if (ModelState.IsValid) 
    { 
     //code is here 
    } 

    // viewname is required, otherwise the view name used will be 
    // the original calling method (ie public1.cshtml, public2.cshtml) 
    return this.View("SharedViewName"); 
    } 

    public ActionResult Public1(SomeModel model) 
    { 
    return this.SharedMethod(model); 
    } 

    public ActionResult Public1(SomeModel model) 
    { 
    return this.SharedMethod(model); 
    } 
} 
+0

其實我有一個視圖,列出了數據庫產品的所有細節。點擊一個按鈕打開一個窗體的彈出窗口。所以我想用這個通用操作方法來發布評論,並將用戶重定向到具有更新評論的相同視圖(產品頁面)。 – sanjeev

相關問題