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?
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.