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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:55:51+00:00 2026-05-28T01:55:51+00:00

We have view models that have other view models in them. For instance, I

  • 0

We have view models that have other view models in them. For instance, I have a navigation view model under just about every other view model, since every screen has navigation. The logic for building the navigation view model is in one place.

The problem is that reference types, like the view models, are null-ed out during POST-ing. This makes sense, but it means we have to partially rebuild the view models if we ever need to go back to the view again, such as when validation fails. We can’t just rebuild the view models from scratch since they hold partially entered data.

Right now, we are manually checking ModelState.IsValid and rebuild each child view model manually. We are eliminating duplicating logic by creating types called Builders that constructs our view models. These builders currently have three build methods: one for building a blank view model, one for handling validation issues and one for handling editing.

ViewModel Build(<params>) // create
void Build(ViewModel, <params>) // validation error
ViewModel Build(DBObject, <params>) // edit

This seems like major overkill. 90% of the time, if a property is another view model, it should just be rebuilt. It would be nice if there was a 3rd party library that would map a view model to a builder class, only constructing them on an as-needed basis. It would be recursive, of course, and build child view models, as well. Instead of:

return View(viewModel) 

or

return RedirectToAction("index", "home", viewModel) 

there would simply be helpers like:

return View<ViewModel>()

or

return RedirectToAction<ViewModel>("index", "home")
  • 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-28T01:55:52+00:00Added an answer on May 28, 2026 at 1:55 am

    For instance, I have a navigation view model under just about every
    other view model, since every screen has navigation. The logic for
    building the navigation view model is in one place.

    That seems like a great candidate for splitting your view models and using the Html.Action helper to render your navigation menu from a separate child action.

    This way you no longer have to worry about the main action and view model.

    So the idea is that you could have a menu controller responsible for generating the menu:

    public class MenuController: Controller
    {
        private readonly IMenuRepository _repository;
        public MenuController(IMenuRepository repository)
        {
            _repository = repository;
        }
    
        [ChildActionOnly]
        public ActionResult Index()
        {
            var menu = _repository.GetMenu();
            var menuViewModel = Mapper.Map<Menu, MenuViewModel>(menu); // AutoMapper example
            return View(menuViewModel);
        }
    }
    

    then the corresponding Index view which will be a partial containing only the menu markup:

    @model MenuViewModel
    @{
        Layout = null;
    }
    <ul>
       <li>....
       <li>....
    </ul>
    

    and then you could render the menu inside your _Layout at its defined location:

    @Html.Action("index", "menu")
    

    Then you could have controllers and view models which are completely dissociated with this menu rendering.

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

Sidebar

Related Questions

if i have created a view model and have a partial form that is
I have a Java project that I'm trying to implement with a model-view-controller design.
So let's say I have a view that access a date: <%= Html.TextBoxFor(model =>
I have two models that are related to each other. For a contrived example,
For instance, I have a model class which handles receiving bluetooth messages from other
I have a view that displays a list of models. Some of the properties
I have a Rails model with various attributes and has_many relationships to other models.
I have a cshtml partial view (Razor engine) that is used to render something
Both OOD (Object-Oriented-Design) and MVC (Model-View-Controller) architectures have become staples of modern software design.
I have a model that represents paintings I present on my site. On 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.