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

このデータを設定する3番目のメソッドprivateを作成し、それを両方のコントローラーメソッドで使用するか、コントローラーで余分なメソッドをあまり作成したくない場合は、静的メソッドを使用して何らかのヘルパークラスを作成します。あなたのためにそれ。とにかく第三に、共有メソッドは洗練されたソリューションです。

于 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 に答える