Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9158303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:12:53+00:00 2026-06-17T13:12:53+00:00

I’m writing my app using Asp.Net MVC 3. In my controller I have two

  • 0

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?

p.s. 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.

Thanks for help in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:12:55+00:00Added an answer on June 17, 2026 at 1:12 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I am writing an app with both english and french support. The app requests
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I am writing an app for my school newspaper, which is run completely online
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.