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

Is it possible to implement a post-redirect-get pattern with two overloaded action methods (one for a GET action and one for a POST action) in .

In all samples of the post-redirect-get MVC template, I saw three different action methods for the post-redirect-get process (corresponding to Initial Get, Post, and Redirection Get), each of which has different names. Is it really necessary to have at least three action methods with different names in ?

For example: (Below is the code shown below after the Post-Redirect-Get template?)

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.

+4
source share
3 answers

Think in terms of unit testing.

If everything was in one action, then the code will be quite difficult to verify and read. I do not see any problems in the code that it was so.

+1
source

Your code seems good to me. Following the pattern, and so we do it in all of our projects.

+1
source

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

Both of these “requirements” in your code are good, so there is no problem using these actions in PRG.

0
source

Source: https://habr.com/ru/post/1433218/


All Articles