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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:33:10+00:00 2026-06-03T13:33:10+00:00

This is more of a high-level question than anything else. I have a MVC

  • 0

This is more of a high-level question than anything else.

I have a MVC project which, among other things, manages users.
I am currently sticking to a more strict approach that on my view pages I only use a model which is declared, meaning that I have a:

@model MVCApp.Models.SomeModel

At the top of each View.cshtml

There are some pages that require more than 1 model. At this point I consolidate the 2 models into 1, and send it to the view as one model.

Here comes the tricky part. Let’s say I have some model which holds the user data. That user data is stored in the session cookies (typical Forms Authentication). It seems weird to me that I now have to wrap each and every model I use with my own model that holds both the User Model and the model which I want to use inside that View.

The question that I ask myself is why not pass the User Model to the ViewBag and use it inside the View. Why is that considered to be bad practice? It allows me to attach that model to every page without having to ultimately duplicate all my models.

I’d love to get some guidance. I might be looking at this the wrong way. Any help will be much obliged.

Thanks,

  • 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-03T13:33:12+00:00Added an answer on June 3, 2026 at 1:33 pm

    There are a couple of reasons why ViewBag should be avoided:

    • ViewBag is weakly typed
    • You don’t get Intellisense in the view
    • Your view is a mixture of ViewBag and a view model and your view gets information from different places instead of centralizing everything into a strongly typed view model
    • ViewBag doesn’t work with strongly typed helpers and expressions because dynamic types cannot be used with extension methods (that’s not a limitation of ASP.NET MVC, it’s .NET => you cannot dispatch extension methods on dynamic types)
    • Due to this weak typing nature of ViewBag you will have to cast in your views turning them into spaghetti code because HTML helpers expect specific types to be passed as arguments
    • … this list goes on (I really don’t have time to fill it but it is very large list)

    So now that we know that ViewBag is bad and shouldn’t be used there are different ways you could solve this requirement by using view models.

    One possibility is to use the Html.Action helper which allows you to insert some portion of HTML in the view by going through a full Controller/Model/View lifecycle. This way you could have a commonly used widget without interfering with your main view model.

    Another possibility is to have a base view model which will contain a property representing user details and which will be populated by a custom global action filter that could executed everytime a controller action has finished executing and returned a view result. The action filter could intercept this view result, read the authentication cookie information and set the view model property. This assumes that all your view models derive from the common base view model. This obviously makes sense if you need to display this user information on each page.

    For example:

    public class UserInfoInjectorAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var result = filterContext.Result as ViewResultBase;
            if (result == null)
            { 
                // the controller action didn't return any view result => no need to continue
                return;
            }
    
            var model = result.Model as BaseViewModel;
            if (model == null)
            {
                // the controller action didn't pass a model or the model passed to the view
                // doesn't derive from the common base view model that will contain 
                // the user info property => no need to continbue any further
                return;
            }
    
            model.UserInfo = ... go ahead and read the forms authentication cookie
                                 userData portion and extract the information
                                 you are looking for
        }
    }
    

    Now all that’s left is to register this action filter as a global action filter and it will be applied on all controller actions.

    Now if all your view models derive from this BaseViewModel you will know that once you arrive in the view the UserInfo property will be populated with the relevant information without polluting all your controller actions with code that does the fetching of this property. And still you get strong typing in the view => no ViewBag (youpeeee).

    Of course depending on your specific scenario there might be other ways to achieve that (obviously not involving any ViewBag).

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

Sidebar

Related Questions

This is a high level/design type question rather than anything specific. In this game
This is more of a high level how do you solve this type of
This is more of a design question. I have a template class, and I
This is more of a language design rather than a programming question. The following
NOTE: This question has been updated to provide more detail and insight than the
This is a high-level question and I am sure there is no universally correct
This is a very high-level question. I'm looking for insight into this problem that
This question is more logic than programming at the moment.. once I understand what
Ok, I'm going to try to make this more clear because my last question
I have a label on my asp.net page, it looks like this: more info

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.