Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6758743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:50:17+00:00 2026-05-26T13:50:17+00:00

Given that web apps should always redirect after a POST (or any non-repeatable request

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:50:18+00:00Added an answer on May 26, 2026 at 1:50 pm

    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);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to host a PHP web site that was given to me. I
I'm looking for a general purpose API/web service/tool/etc... that allows convert a given HTML
Given that Sql Server Reporting Services is designed to be open and extendable, and
I'm working with a web service that will give me values like: var text
Can anyone give me pointers to good books or web sites that teach how
Given that indexing is so important as your data set increases in size, can
Given that Chrome and Safari use webkit has anyone yet found anything that renders
Given that a function a_method has been defined like def a_method(arg1, arg2): pass Starting
Given that I only have one monitor, what's the best way to debug a
Given that Decimal.MaxValue = 79228162514264337593543950335m Why does the next line give me 7922816251426433759354395034M in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.