简体   繁体   中英

How to withdraw code from an action method into helper in Asp.Net MVC 3?

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:

    [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 
    }

So, I want to withdraw all this code into helper method.

What I've already tried:

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:

    [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);
    }

The EditPost code is almost the same. The view name is "EditPost".

When I run the app and AddPost method executes the validation works and the new post is created but this line never executes:

return RedirectToAction("NewsFeed");

So I'm redirected to "AddPost" view again and again.

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:

[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");
}

So, How could I refactor my code so my action methods (EditPost and AddPost) would not contain the same chunk of code?

I need different views for AddPost and EditPost methods cause the "back to content" links in them are different. 我需要AddPost和EditPost方法使用不同的视图,因为它们中的“返回目录”链接不同。 So, I can't just redirect to the EditPost view from AddPost method.

Thanks for help in advance!

Just put your "back to content" link in the model, then use the same view for both, then you can use the same HttpPost method. Saves having to duplicate everything.

I would solve it like this:

  1. I would withdraw the method implementation into separate private method. This method will be invoked by each of the public action methods. Since the View name differs for both methods I would pass the view name as parameter to the private method.
  2. The private method doesn't need the HttpPostAttribute!
  3. Don't forget to declare Add and Edit action methods as returning ActionResult! As parameter they will expect only Post, the view name has to be hard-coded into the action methodsiteslf ;-)

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM