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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:47:32+00:00 2026-06-10T06:47:32+00:00

I have a foreach looping through a List of objects in an ASP.NET MVC/Razor

  • 0

I have a foreach looping through a List of objects in an ASP.NET MVC/Razor project. I’m trying to use to index of the current object to determine if there is another object in the list.

Every time through the loop, I’m getting -1 as the value returned from Model.TodaySoups.IndexOf(soup) in the following code:

@foreach(MenuItem soup in Model.TodaysSoups)
{
    int i = Model.TodaysSoups.IndexOf(soup); 
    string comma = i < Model.TodaysSoups.Count ? "," : "";
    <li class="soup">@soup.Name@comma </li>
}

TodaysSoups is defined as:

public List<MenuItem> TodaysSoups 

I’ve also tried defining soup implicitly:

var soup in Model.TodaysSoups

didn’t help.

As far as I know, this should work. Clearly, I mistaken. Any help would be appreciated.

Update:
Any off by one issues you can ignore, I’ve been staring at this for a while and just trying whatever I could think of, but, thank you for trying to help me with them. The weird part is that I’m getting -1 every time.

The MenuItem definition

 public class MenuItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
    public List<MenuOption> Options { get; set; }
    public bool IsAvailable { get; set; }
    public bool IsSpecial { get; set; }

    public MenuItem()
    {
        this.IsAvailable = true;
        this.IsSpecial = false;
        this.Options = new List<MenuOption>();
    }
}

Worth noting:
I had originally planned on using EF, but we’re using the Enterprise Library 5.0 DAAB at work, so I switched over so I could get used to using that. I then ripped out what I believed to be all references to EF in my code and config files, and there’s a very real possibility that I missed something, because I’m really not familiar with it. Possible cause?

  • 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-10T06:47:34+00:00Added an answer on June 10, 2026 at 6:47 am

    Quick fix: use a for-loop.

    @for(int i = 0; i < Model.TodaySoups.Count; i++)
    {
        MenuItem soup = Model.TodaySoups[i];
        // now you have 'soup' and 'i'
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a checkbox list that isn't updating on the looping through it. <asp:CheckBoxList
Possible Duplicate: How does foreach work when looping through function results? If I have
I have a multidimensional array which I am looping through using a foreach loop.
I have a list of Map.Entry<String,Integer> s that I am looping through, and for
I have a collection lets say list of integers and looping through it using
Summary: I have a list of policy numbers I'm looping through in Column A
I have a For Each loop that is looping through an array of strings
i have two foreach loop to display some data, but i want to use
I have a foreach loop in jQuery going through each 'div.panel', I need to
I'm trying to write some abstract code for searching through a list of similar

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.