I have the following action
[GET("Foo")]
public virtual ActionResult Foo()
{
return View(new FooViewModel());
}
the view for this action calls this partial view
@{ Html.RenderAction(MVC.FooBar.AddFoo()); }
with controller actions
[ChildActionOnly]
[GET("Foo/Add")]
public virtual ActionResult AddFoo()
{
var viewModel = new AddFooViewModel();
return PartialView(viewModel);
}
[POST("Foo/Add")]
public virtual ActionResult AddFooPost(AddFooViewModel viewModel)
{
// If ModelState is invalid, how do I redirect back to /Foo
// with the AddFooViewModel ModelState intact??
if (!ModelState.IsValid)
return MVC.FooBar.Foo();
// ... persist changes and redirect
return RedirectToAction(MVC.FooBar.Foo());
}
If somebody submits the AddFoo form with ModelState errors, I want the POST action to redirect back to /Foo and show the AddFoo partial view with the ModelState errors. What's the best approach to handle this?