Search code examples
c#asp.netasp.net-mvcvisual-studio

foreach statement cannot operate on variables of type 'Oblig1.Models.User' because it does not contain a public definition for 'GetEnumerator'


I keep getting this error and can't seem to find where I am going wrong. Can anybody lend a hand?

This is my view, Minside.cshtml:

@model Oblig1.Models.User
<br />
<br />
<br />

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li class="active"><a href="#home" role="tab" data-toggle="tab">Kontoinformasjon</a></li>
<li><a href="#profile" role="tab" data-toggle="tab">Ordrehistorikk</a></li>
</ul>

<!-- Tab panes -->
<div class="tab-content">
    <div class="tab-pane active" id="home">
        <br />
        <br />
        <table class="table">
            @foreach (var user in Model)
            {
                @user.Firstname
            }
        </table>
    
    </div>
    <div class="tab-pane" id="profile">Ordrehistorikk</div>
    <div class="tab-pane" id="messages">...</div>
    <div class="tab-pane" id="settings">...</div>
</div>

Controller:

    public ActionResult Minside()
    {
        var db = new PastaContext();
        
        string compareEmail = (string)Session["User"];

        User foundUser = db.Users.Find(compareEmail);

        if (foundUser == null)
        {
            return RedirectToAction("Index");
        }
        else
        {
            return View(foundUser);
        }           
    }

I have tried to change the first line in the view to @model IEnumerable<Oblig1.Models.User> as well.

All I really want to happen is that I list out the information of a single user in the view.


Solution

  • You state at the top of your file that the model for the MVC template is a User

    @model Oblig1.Models.User
    

    So you don't need the foreach, just type FirstName directly in your view.

    @model.FirstName