Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 model validation best practice redirect after post / state change

Given that web apps should always redirect after a POST (or any non-repeatable request to change server-side state) ...

... how are people using MVC3 Model Validation and performing the mandatory redirect?

like image 986
Jack Avatar asked Oct 29 '11 18:10

Jack


1 Answers

Usually you only redirect after a successful post (no Model Validation errors), otherwise you send back the page with a validation error message.

The redirect in the PRG pattern prevents double-posting, so there's no harm to send back the same page (+ error message) because the post was not successful and will not be unless something changes to make validation pass.

Edit:

It looks like you're looking for passing ModelState to the next (redirected) request. This can be done by using TempData to store ModelState up to the next request. FYI, TempData uses Session.

This can be implemented with ActionFilters. Examples can be found in the MvcContrib project code: ModelStateToTempDataAttribute

This has also been mentioned together with other tips in a 'best practices' article on weblogs.asp.net (seems the author has moved the blog, but I couldn't find the article on the new blog). From the article:

One of the issue with this pattern is when a validation fails or any exception occurs you have to copy the ModelState into TempData. If you are doing it manually, please stop it, you can do this automatically with Action Filters, like the following:

Controller

[AcceptVerbs(HttpVerbs.Get), OutputCache(CacheProfile = "Dashboard"), StoryListFilter, ImportModelStateFromTempData]
public ActionResult Dashboard(string userName, StoryListTab tab, OrderBy orderBy, int? page)
{
    //Other Codes
    return View();
}

[AcceptVerbs(HttpVerbs.Post), ExportModelStateToTempData]
public ActionResult Submit(string userName, string url)
{
    if (ValidateSubmit(url))
    {
        try
        {
            _storyService.Submit(userName, url);
        }
        catch (Exception e)
        {
            ModelState.AddModelError(ModelStateException, e);
        }
    }

    return Redirect(Url.Dashboard());
}

Action Filters

public abstract class ModelStateTempDataTransfer : ActionFilterAttribute
{
    protected static readonly string Key = typeof(ModelStateTempDataTransfer).FullName;
}

public class ExportModelStateToTempData : ModelStateTempDataTransfer
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //Only export when ModelState is not valid
        if (!filterContext.Controller.ViewData.ModelState.IsValid)
        {
            //Export if we are redirecting
            if ((filterContext.Result is RedirectResult) || (filterContext.Result is RedirectToRouteResult))
            {
                filterContext.Controller.TempData[Key] = filterContext.Controller.ViewData.ModelState;
            }
        }

        base.OnActionExecuted(filterContext);
    }
}

public class ImportModelStateFromTempData : ModelStateTempDataTransfer
{
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        ModelStateDictionary modelState = filterContext.Controller.TempData[Key] as ModelStateDictionary;

        if (modelState != null)
        {
            //Only Import if we are viewing
            if (filterContext.Result is ViewResult)
            {
                filterContext.Controller.ViewData.ModelState.Merge(modelState);
            }
            else
            {
                //Otherwise remove it.
                filterContext.Controller.TempData.Remove(Key);
            }
        }

        base.OnActionExecuted(filterContext);
    }
}
like image 76
marapet Avatar answered Dec 09 '22 23:12

marapet