asp.net-corevariablesforeachsyntaxrazor-pages

Understanding Error CS1579 foreach statement cannot operate on variables of type


Getting this error in my razor page app:

Severity Code Description Project File Line Suppression State Error (active) CS1579 foreach statement cannot operate on variables of type 'WorkingVote' because 'WorkingVote' does not contain a public instance or extension definition for 'GetEnumerator' VoteTracker S:\VisualStudioProjects\VoteTracker\Pages\WorkingVotes\Edit.cshtml

The line it is occurring on is in my table body on Edit.cshtml.

<tbody>
    @foreach (var obj in Model.WorkingVote)
    {...

The Model.WorkingVote is underlined.

I looked up this error and also read all posts here about that error, but do not see how to directly apply it to my app. I know it's looking for 'Get Enumerator', and needs some type of collection? I've tried changing the 'public WorkingVote? WorkingVote to an IEnumerable or List, but that gives me even more errors.

Here is my .cs:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;

namespace VoteTracker.Pages.WorkingVotes;

[BindProperties]

public class EditModel : PageModel
{
    private readonly ApplicationDbContext _db;
    public EditModel(ApplicationDbContext db)
    {
        _db = db;
    }

    public WorkingVote? WorkingVote { get; set; } 

    public IEnumerable<Committee> DisplayCommitteeData { get; set; }
    public IEnumerable<VoteType> DisplayVoteTypeData { get; set; }
    public IEnumerable<Supervisor> DisplaySupervisorData { get; set; }
    public IEnumerable<Status> DisplayStatusData { get; set; }

    public async Task OnGetAsync(int Id)
    {
        WorkingVote = _db.WorkingVote.Find(Id);
        DisplayCommitteeData = await _db.Committee.ToListAsync();
        DisplayVoteTypeData = await _db.VoteType.ToListAsync();
        DisplaySupervisorData = await _db.Supervisor.ToListAsync();
        DisplayStatusData = await _db.Status.ToListAsync();
    }

    public async Task<IActionResult> OnPost()
    {
        if (ModelState.IsValid)
        {
            _db.WorkingVote.Update(WorkingVote);
            await _db.SaveChangesAsync();
            TempData["success"] = "Vote cast successfully.";
            return RedirectToPage("Index");
        }
        return Page();
    }
}

I've been kicking this around for a while testing different things, but could sure use some assistance. Thank you!!


Solution

  • In your code, WorkingVote is a single object rather than a collection. If you want to iterate over a WorkingVote collection in the page, you need to define a collection of IEnumerable<WorkingVote> in your EditModel. Here is an example for your reference:

    public class EditModel : PageModel
    {
        private readonly ApplicationDbContext _db;
        public EditModel(ApplicationDbContext db)
        {
            _db = db;
        }
    
        public IEnumerable<WorkingVote> WorkingVotes { get; set; }
    
    
    
        public async Task OnGetAsync(int Id)
        {
            WorkingVotes = await _db.WorkingVotes.Where(v => v.Id == Id).ToListAsync();
    
        }
    
        public async Task<IActionResult> OnPost()
        {
            if (ModelState.IsValid)
            {
                _db.WorkingVotes.UpdateRange(WorkingVotes);
                await _db.SaveChangesAsync();
                TempData["success"] = "Vote cast successfully.";
                return RedirectToPage("Index");
            }
            return Page();
        }
    }
    

    view:

     @page "{id:int}"
        @model EditModel
        
        <tbody>
            @foreach (var obj in Model.WorkingVotes)
            {
                <tr> @obj.Name </tr>
            }
        </tbody>
    

    When I query the id 1:

    enter image description here enter image description here