Search code examples
asp.net-mvcmodel-view-controllerdesign-patternsweb-applicationspost-redirect-get

ASP.NET MVC: post-redirect-get pattern, with two overloaded action methods


Is it possible to implement post-redirect-get pattern, with two overloaded action methods (One for GET action and the other for POST action) in .

In all of the MVC post-redirect-get pattern samples, I have seen three different action methods for the post-redirect-get process (corresponding to Initial Get, Post, and the Redirection Get), each having different names. Is this really required to have minimum three action methods with different names, in ?

For Eg: (Does the code shown below, follows Post-Redirect-Get pattern?)

public class SomeController : Controller
{
    // GET: /SomeIndex/
    [HttpGet]
    public ActionResult Index(int id)
    {
        SomeIndexViewModel vm = new SomeIndexViewModel(id) { myid = id };
        //Do some processing here
        return View(vm);
    }

    // POST: /SomeIndex/
    [HttpPost]
    public ActionResult Index(SomeIndexViewModel vm)
    {
        bool validationsuccess = false;
        //validate
        if (validationsuccess)
            return RedirectToAction("Index", new {id=1234 });
        else
            return View(vm);
        }
    }
}

Thank you for your responses.


Solution

  • Think from the unit-testing perspective.

    If everything was in a single action, then code would be quite difficult to test and read. I see no problems in your code what so ever.