2010-08-24 32 views
1

我有以下代碼:ASP.NET MVC:將數據發送到意見POST請求

public ActionResult Foo() 
    { 
     var a = "a"; 


     return View(new FooModel { A = a}); 

    } 

    [HttpPost] 
    public ActionResult Foo(....) 
    { 
     // I need to set all the values of the ViewModel again, not to get a null exception in the view 
     return View(new FooModel { A = a}); 
    } 

所以,我怎麼能保持乾燥和不重複,我已經做了的東西?

回答

1

創建第三個方法,私有,將設置此數據給你,然後在這兩個控制器的方法使用它,或者,如果你不想讓你的控制器太多額外的方法創建某種有靜態輔助類方法將返回給你。總之第三,共享方法是一個優雅的解決方案。

-1

可能,這將是愚蠢的,但它的工作原理:)

[HttpPost] 
    public ActionResult Foo(....) 
    { 
     // I need to set all the values of the ViewModel again, not to get a null exception in the view 
     return Foo(); 
    } 
+0

你怎麼能知道它的工作原理,如果你不知道他是如何在視圖中使用它? – 2010-08-24 12:17:29

+0

我說,這是愚蠢的;但是這個語法也適用。我只是想知道,我可以在一個操作方法再次調用的操作方法。所以我無法理解你。你的答案當然是正確的,這只是一個實驗。 – 2010-08-24 12:50:56