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

  • Home
  • SEARCH
  • 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 3988352
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:14:30+00:00 2026-05-20T06:14:30+00:00

Say I have the following code, mocked up in notepad, so excuse any minor

  • 0

Say I have the following code, mocked up in notepad, so excuse any minor errors 🙂

//Default page
public ActionResult Index()
    {
        var musicViewModel
        {
         Albums = GetTopSellingAlbums(5),
         Genres = GetTopGenres(5),
         Artists = GetTopArtists(5)
         };

        return View(musicViewModel);
    }

[HttpPost]
public ActionResult Index(MusicViewModel musicViewModel)
    {

        //For the example, pretend I have a class called musicStoreSubmission in my
        //viewmodel which holds a few different fields the user fills out.

        if(ModelState.IsValid)
        {
            //Do some actions based on the user submitting a form
        }

        //Else, refresh page with errors in Modelstate.
        var musicViewModel
        {
         Albums = GetTopSellingAlbums(5),
         Genres = GetTopGenres(5),
         Artists = GetTopArtists(5)
         };

        return View(musicViewModel);
    }

My point of concern is that in order to postback any errors with the ModelState being invalid, I need to generate the viewmodel again so that any elements on the page that use those objects can be created (genres, artists etc.). The problem is it requires me copying and pasting some of the code from ActionResult to ActionResult, seemingly making my code not very DRY.

Is there a better way of avoiding repeated code like this? At the moment I have simply moved the generation of any default objects the viewmodel needs into a separate method and/or the constructor, but it’s a bit messy since I have to generate all the objects I might need for the entire controller. What I was hoping I could do would be to point my second Index Action to the first Index Action and just use that as a regular method. I’ve tried a few different ways of doing this though and cant seem to return an ActionResult into another ActionResult.

Any thoughts?

  • 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-05-20T06:14:31+00:00Added an answer on May 20, 2026 at 6:14 am

    You can return an another ActionResult method like this:

    [HttpPost]
    public ActionResult Index(MusicViewModel musicViewModel)
    {
        if(ModelState.IsValid)
        {
            //Do some actions based on the user submitting a form
        }
        return MyAction();
    }
    

    or you could pass the posted model back to ViewResult

    [HttpPost]
    public ActionResult Index(MusicViewModel musicViewModel)
    {
        if(ModelState.IsValid)
        {
            //Do some actions based on the user submitting a form
        }
        return View(musicViewModel);
    }
    

    The second approach is better as you don’t rebuild the ViewModel

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

Sidebar

Related Questions

Lets say I have the following code: public class Base { // Some stuff
Lets say I have the following code: public class Collection implements CollectionInterface{ ElementInterface[] elementArray
Say I have the following Code: public static DependencyProperty LabelProperty = DependencyProperty.RegisterAttached( Label, typeof(Label),
Say I have the following code: import java.lang.InterruptedException; import javax.swing.SwingWorker; public class Test {
say I have the following code: public class Pond { public List<Frog> Frogs {
Say I have the following code: public class ClassToTest { AnotherClass anotherClass; public void
Let's say I have the following code within another method: Parallel.For(0, pageCount, page =>
Say have the following code: public void SaveOrUpdate(OrderContract orderContract) { foreach (var orderContract in
Lets say i have the following code private Func<T> _method; public void SetExecutableMethod<T>(Func<T> methodParam)
Let's say I have the following code on my view page (in asp.net mvc

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.