2010-08-24 49 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