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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:29:13+00:00 2026-06-15T11:29:13+00:00

When I build my views, I’d like to eliminate as much processing logic as

  • 0

When I build my views, I’d like to eliminate as much processing logic as possible. Ideally, I’d like to stick to just HTML and rendering values with Razor.

So suppose I have a FooModel with a list of BarModel objects:

public class FooModel
{
    public List<BarModel> Bars { get; set; }

    public string FoosFirstValue { get; set; }
    public string FoosOtherValue { get; set; }
}

public class BarModel
{
    public string SomeValue { get; set; }
    public string SomeOtherValue { get; set; }
}

And in my view, I need to loop through and display elements on my Bars:

@model MyApp.FooModel
<div>@Model.FoosFirstValue</div>
<div>@Model.FoosOtherValue </div>
<div>
    Bars:
    <ul>
        @foreach (var bar in Model.Bars)
        {
            <li>@bar.SomeValue: @bar.SomeOtherValue</li>
        }
    </ul>
</div>

Is there anyway to avoid using a loop in my view?

  • 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-15T11:29:14+00:00Added an answer on June 15, 2026 at 11:29 am

    While looping in an MVC view isn’t necessarily a bad thing, some purists (like you seem to be) prefer to keep their views super clean and void of any processing logic.

    Fortunately for you, there is a little known jewel in MVC that it can take an IEnumerable list of objects, and render that to a editor/display template bound to the singular object — it handles the looping for you.

    For example, if you set up a display template partial view, let’s call it BarModel.cshtml (should be stored in one of the PartialViewLocationFormats search paths, under a “DispalyTemplate” sub-folder):

    @model MyApp.BarModel
    <li>@Model.SomeValue: @Model.SomeOtherValue</li>
    

    Now, in your view, you can simply call out to the display template using Html.DisplayFor, and pass in the full list as the model:

    <div>
        Bars:
        <ul>
            @Html.DisplayFor(m => m.Bars)
        </ul>
    </div>
    

    And that’s it. It will take your list of BarModels, and generate the partial display tempalte for each BarModel object in the list.

    Of course, with your simple example, it’s really down to personal preference, as there’s not a major benefit, unless you simply just want to get rid of the looping logic.

    But if you ever need to share this display template in multiple locations on the same view, or in different views, it really pays off to build your views like this and reduce complexity and code duplication.

    One other side note: This approach assumes that you want to display every item in the list, and in the order it is stored in the list. If you need to conditionally display items, or want to display in a different order, this approach won’t work.

    However, if this is the case, I would suggest setting up the list exactly as you need it displayed in the view. That is, do any filtering/sorting within the controller/service that builds the list, and leave your view to just rendering you models.

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

Sidebar

Related Questions

I'm trying to build a form with nested attributes and in my view views/pedidos/new.html.erb
im trying to build an application like a book, with so many custom views
I want a visual tool to build the html for my ASP.NET MVC views,
I just want to make sure this is possible. Let's say I build three
I quite often use Drupal's Views Module to build SQL that I paste into
I would like to build a generic/re-usable modal dialog that I can use in
I build my first iPhone application and I have a problem with switching views.
I have build Views that show some images from nodes. I'm also using Views
In my applications, I often rely on custom build views, such as in the
I'm creating a build script for a database and building the views in alphabetical

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.