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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:11:24+00:00 2026-05-14T22:11:24+00:00

For anything but trivial view models, I use a view model builder that handles

  • 0

For anything but trivial view models, I use a view model builder that handles the responsibility of generating the view model object. Right now, I use constructor injection of the builders into my controllers but this smells a little since the builder is really dependent upon which action method is being executed. I have two ideas in mind. The first one would involve a custom ActionFilter allowing me to decorate each action method with the appropriate builder to use. The second would be to add an override of the View method that is open to accepting a generic.

This is what my code currently looks like. Note, the builder get injected via the ctor.

    [HttpGet, ImportModelStateFromTempData, Compress]
    public ActionResult MyAccount()
    {
        return View(accountBuilder.Build());
    }

Here is what option one would look like:

    [HttpGet, ImportModelStateFromTempData, Compress, ViewModelBuilder(typeof(IMyAccountViewModelBuilder)]
    public ActionResult MyAccount()
    {
        return View();
    }

Or option two:

    [HttpGet, ImportModelStateFromTempData, Compress]
    public ActionResult MyAccount()
    {
        return View<IMyAccountViewModelBuilder>();
    }

Any thoughts or suggestions would be great!

  • 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-14T22:11:24+00:00Added an answer on May 14, 2026 at 10:11 pm

    TheBuilder

    I think you could move the responsibility of construct the correct view model to the builder. and you could pass the ViewModel type you want to build as a parameter, something like:

    [HttpGet, ImportModelStateFromTempData, Compress]
    public ActionResult MyAccount()
    {
       return View( AccountBuilder.Build<MyAccountViewModel>( ) );
    }
    

    About candidates

    FirstOptionThis

    the above approach allows you to have more flexibility in the view you will render inside the action (what happens if your controller should choose between 3 views to show, and each one has a different view model? Filter solutions start to get complex.)

    SecondOption

    The View method responsibility is to take the model and render the view using it. it’s the controller responsibility to build the model. so being a little orthodox, I would recommend to avoid putting model building logic in the View method : ).

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

Sidebar

Related Questions

Not that I'm trying to prevent 'View Source' or anything silly like that, but
Eclipse is driving me nuts right now. It's probably something trivial but I just
I'm trying to get a regex that will match: somefile_1.txt somefile_2.txt somefile_{anything}.txt but not
OK so that title sucks a little but I could not think of anything
I see lot of example opening popup with jquery but don't see anything that
I tried searching around, but I couldn't find anything that would help me out.
I was wondering what are the benefits of using anything else but Java for
I don't know anything about XPIs but I've searched through the install.rdf, *.js, and
More out of interest than anything else, but can you compile a DirectX app
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.