Question

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.

Était-ce utile?

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

Autres conseils

Your code seems fine to me. Follows the pattern and this is how we do it in all of our projects.

If you have the same name for the action then you should separate which action is GET and which is POST. Also, your method signature must be different to avoid a compilation error.

Both of these "requirements" are okay in your code, so there is no problem using those actions in the PRG.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top