Imran
Imran

Reputation: 13

Error:A local variable named 'Model' cannot be declared in this scope

I m returing a List to the View. So The Model is IEnumerable. It works fine in foreach loop to get the employees. But when I use this Model inside the loop it give me the error:

A local variable named 'Model' cannot be declared in this scope because it would give a different meaning to 'Model', which is already used in a 'parent or current' scope to denote something else.

Inside the loop when I use other name other than Model it works fine.

View @model IEnumerable

@foreach (tbEmployee emp in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(Model => emp.empID)
            </td>
            <td>
                @Html.DisplayFor(Model  => emp.empName)
            </td>
            <td>
                @Html.DisplayFor(Model  => emp.empAge)
            </td>
            <td>
                @Html.DisplayFor(Model  => emp.empStatus)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", "Employee")
                @Html.ActionLink("Update", "Update", "Employee")
                @Html.ActionLink("Delete", "Delete", "Employee")
            </td>
        </tr>
    }

But When I write like, it works fine

 @foreach (tbEmployee emp in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(x => emp.empID) //where this x get data from
            </td>
            <td>
                @Html.DisplayFor(x => emp.empName)
            </td>
            <td>
                @Html.DisplayFor(x => emp.empAge)
            </td>
            <td>
                @Html.DisplayFor(x => emp.empStatus)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", "Employee")
                @Html.ActionLink("Update", "Update", "Employee")
                @Html.ActionLink("Delete", "Delete", "Employee")
            </td>
        </tr>
    }

Upvotes: 1

Views: 1678

Answers (1)

Eric Lippert
Eric Lippert

Reputation: 659956

First off, this error message has been removed in more recent versions of C#; the design team decided that it created more confusion than prevented errors.

The issue is that the name Model is here used to mean two things: whatever property or variable is used as the collection of the foreach, and as a formal parameter of a lambda. Moreover, the two usages overlap. That is, the Model that is used in the foreach is directly outside the usages of Model as a formal inside the foreach. Basically the compiler here is saying "you are creating a situation where you're possibly using the same name to mean two completely different things right next to each other" and that is a very confusing and bug prone situation to be in.

(Having two lambdas beside each other with the same names for the formal parameters is not illegal because they do not overlap. If one lambda was in another, that would give a similar error.)

This error message is well-intentioned but regrettably, the compiler does a poor job of explaining the problem to the user. I did some work to improve that error message in... 2011 maybe? Or thereabouts. But the improvements were marginal at best.

As you've discovered, the correct fix is stop using the same name to mean two completely different things in the same scope.

Upvotes: 4

Related Questions