views:

31

answers:

2

I have the following code:

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

So how can I keep it DRY and not repeat stuff that I have already done?

+1  A: 

Create a third method, private, that will set this data for you, then use it in both your controller methods or if you do not want to make too much extra methods in your controller create some kind of helper class with static methods that will return it for you. Anyway third, shared method is an elegant solution.

ŁukaszW.pl
A: 

May be, it will be silly, but it works :)

[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();
    }
bahadir arslan
How can you tell if it works if you do not know how he use it in a view?
ŁukaszW.pl
I said that, this is silly; but this syntax works, too. I just wonder, can i call an Action method again in an Action method. So i couldn't understand you. Your answer is the right one of course, this is only an experiment.
bahadir arslan