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 8787145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:48:46+00:00 2026-06-13T21:48:46+00:00

I have 2 ActionResults that return the same view with a DropDownList source Public

  • 0

I have 2 ActionResults that return the same view with a DropDownList source

Public ActionResult Create()
{
    var model = new ViewModel {
        Entity = new Entity(),
        Categories = GetCategories()
    };

    return View("Edit", model);
}

Public ActionResult Edit(int id)
{
    var model = new ViewModel {
        Entity = GetFromDatabase(id),
        Categories = GetCategories()
    };

    return View(model);
}

I feel like I’m breaking the DRY principle, even if I’ve moved the population of the categories to a method. Is there a better way to go about this and only state where to get the Categories from once?

  • 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-13T21:48:47+00:00Added an answer on June 13, 2026 at 9:48 pm

    I think you are bit over concerned. This code looks fine for me. This is more readable. As long as you don’t have a a big performance issue, you don’t need to worry about that.

    If you still want to avoid GetCategories call in 2 places and you may put that into the constructor of your ViewModel class.

    public class ViewModel
    {
      public ViewModel()
      {
      }
      public ViewModel(bool includeCategories)
      {
        this.Categories=SomeService.GetCategories();
      }
    
      public List<SelectListItem> Categories { set;get;}
      //other properties
    }
    

    It is up to you how to handle this. There are no written rules. Follow what you think is more readable and look clean. But personally, i would keep my ViewModel as simple POCO class without any of this constructor logic to load data. I would be happy with calling GetCategories in both the action methods. To me, that looks clean and readable.

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

Sidebar

Related Questions

i have 2 actions public ActionResult FilesAdd(int id) { FillParentMenuDDL(id); return View(); } [HttpPost]
I trying to return the same model back to the view that edited the
I have an ASP.NET MVC form that when submitted can return either an ActionResult
I have the following action methods: public ActionResult ProfileSettings() { Context con = new
I have a code similar to this: My View @model MyCode.Models.RemarksModel foreach (var row
I have an several controllers where I want every ActionResult to return the same
I have some action for example public ActionResult SomeAction(Model login) { ..... //Login fail
I have an ActionResult that returns a View with a grid that show notes
I have a simple question. I have a model that looks like this: public
I have several views that submit to the same controller's ActionResult and would like

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.