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>
}
Modelin the lambda like that, the error message makes it clear.Modelalready exists, so when you useModelin the lambda you are effectively creating another variable with the same name. So just usexor anything else that isnt already taken. To test, look at the output, its what you want right? - maccettura