Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to withdraw code from an action method into helper in Asp.Net MVC 3?
    primarykey
    data
    text
    <p>I'm writing my app using Asp.Net MVC 3. In my controller I have two action methods with the very same code apart from one line. Here it is: </p> <pre><code> [HttpPost] public ActionResult EditPost(Post post) { if (ModelState.IsValid) { _postsRepository.UpdatePost(post); return RedirectToAction("NewsFeed"); } return View("EditPost", post); } [HttpPost] public ActionResult AddPost(Post post) { if (ModelState.IsValid) { _postsRepository.UpdatePost(post); return RedirectToAction("NewsFeed"); } return View("AddPost", post); // the return view is different } </code></pre> <p>So, I want to withdraw all this code into helper method. </p> <p>What I've already tried: </p> <p>1) I tried to put all the code into helper method and pass as parameters ModelState.IsValid and View name. And then in AddPost and EditPost I call this helper method instead of code listed above. Here is the new code: </p> <pre><code> [HttpPost] // also tried without this attribute public ActionResult HelperPost(Post post, string viewName, bool modelState) { if (modelState) { _postsRepository.UpdatePost(post); return RedirectToAction("NewsFeed"); } return View(viewName, post); } [HttpPost] // also tried without this attribute public void AddPost(Post post) { HelperPost(post, "AddPost", ModelState.IsValid); } </code></pre> <p>The EditPost code is almost the same. The view name is "EditPost".</p> <p>When I run the app and AddPost method executes the validation works and the new post is created but this line never executes: </p> <pre><code>return RedirectToAction("NewsFeed"); </code></pre> <p>So I'm redirected to "AddPost" view again and again.</p> <p>2) Also tried to redirect to HelperPost method instead of calling it withing AddPost and EditPost. The result is still the same: seems like RedirectToAction("NewsFeed") doesn't execute. (Here I neglected the validation just to simplify the example, cause I would have to create new model with properties: Post post, string viewName, bool modelState). The code: </p> <pre><code>[HttpPost] // tried without attribute public void AddPost(Post post) { return RedirectToAction("HelperPost", post); } [HttpPost] // tried without attribute public RedirectToRouteResult HelperUpdatePost(Post post) { _postsRepository.UpdatePost(post); return RedirectToAction("NewsFeed"); } </code></pre> <p>So, How could I refactor my code so my action methods (EditPost and AddPost) would not contain the same chunk of code? </p> <p><b>p.s.</b> I need different views for AddPost and EditPost methods cause the "back to content" links in them are different. So, I can't just redirect to the EditPost view from AddPost method. </p> <p>Thanks for help in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload