1

我有以下代码:

    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});
    }

那么我怎样才能让它保持干燥而不重复我已经做过的事情呢?

4

2 回答 2

1

创建第三个方法,私有的,它将为您设置此数据,然后在您的控制器方法中使用它,或者如果您不想在控制器中创建太多额外的方法,请创建某种带有静态方法的辅助类,这些方法将返回它给你。无论如何,共享方法是一个优雅的解决方案。

于 2010-08-24T10:12:30.603 回答
-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();
    }
于 2010-08-24T11:05:01.953 回答