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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:59:45+00:00 2026-05-30T18:59:45+00:00

How can I reuse my declared model type? At the top of the view

  • 0

How can I reuse my declared model type? At the top of the view there is the declaration

@model mySpace.viewModels.viewModel

What I want to do is later on in the page, do something like

@for( viewModel vM in ViewBag.viewModels )
{
 //some foo bar
}
  • 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-30T18:59:47+00:00Added an answer on May 30, 2026 at 6:59 pm

    If you already have a strongly typed view, you really shouldn’t be even thinking about ViewBag/ViewData. Once you have strongly typed view to a view model all you have to do in this view is use the properties of this view model, like so:

    @foreach (viewModel vm in Model.SomeSubModels)
    {
        //some foo bar
    }
    

    or even better, to avoid the horrible foreach loops in your views, you could use using editor templates (if you need to edit some properties):

    @Html.EditorFor(x => x.SomeSubModels)
    

    or using a display template (if you only want to display values of your view model properties to the user):

    @Html.DisplayFor(x => x.SomeSubModels)
    

    So here’s are a couple of basic rules to follow when designing an ASP.NET MVC application (in that order):

    1. you forget about ViewBag/ViewData. It’s as if they didn’t exist. You simply wipe them out of existence. You will do a real service to yourself if you forget about them.
    2. you define a view model that is a class containing all the properties that your view will need.
    3. you have your controller action query your repositories and domain models and map the results to this view model that will be passed to the view.
    4. in the corresponding view you simply use the properties of the view model that the controller action provided to display some information.

    Rule number 1. is the real basics and the most important one. If you don’t respect this rule, you really aren’t doing ASP.NET MVC correctly.

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

Sidebar

Related Questions

I want to reuse my old ASP.NET WebForm Master Page in new ASP.NET MVC
Is there a way I can reuse Styles for more than 1 TargetType eg.
Is it possible to permanently store a cookie so I can reuse it? For
How would I store the result of a select statement so I can reuse
I use this code to update data in database table. Can reuse same code
How can I reuse the application icon from within my application so I don't
How can we re-use the deleted id from any MySQL-DB table? If I want
I'm writing a jQuery plug-in so I can reuse this code in many places
Using Glib Testing framework, I would like to know if I can reuse a
How can I create a html snippet that I can reuse across multiple template

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.