Search code examples
c#asp.netasp.net-mvcdatabaseentity

Compilation Error CS0103 in ASP.NET MVC3


I'm working on a project with ASP.NET and SQL Server 2012, and when I try to load the project it says:

Compilation Error CS0103: The name 'model' does not exist in the current context

This is my code:

@model IEnumerable<SegundoParcialP3.Models.Productos>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
@using(Html.BeginForm())
{}
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Nombre)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Precio)
        </th>
        <th></th>
    </tr>

@foreach (var item in model) {
    <tr> 
        <td>
            @Html.DisplayFor(modelItem => item.Nombre)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Id)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Precio)
        </td>
    </tr>
}
    <tr> 
        <td>
            @Html.ActionLink("Edit", "Edit") |@*, new { id=item.Id }*@
            @Html.ActionLink("Details", "Details") |@*, new { id=item.Id }*@
            @Html.ActionLink("Delete", "Delete") | @*, new { id=item.Id }*@
        </td>
    </tr>


</table>

Solution

  • Your problem lies here:

    @foreach (var item in model) {
    

    change it to:

    @foreach (var item in Model) {
    

    lowercase model is not anything, uppercase is a member of the ViewPage class.

    As a side note, it would have been easier to find this if you hadn't used the word "model" virtually everywhere... When you have these problems, start renaming things when you can. That can help narrow it down.