'how to fix duplicate Displayed records in table in ASP.NET Core MVC?
I'm trying to display records to View but it shows duplicate records. I try to debug in the Controller, the records are correctly retrieved from the database no prob The problem is in the Views.
<tbody>
@if (Model == null)
{
<tr>
<td colspan="7" class="text-center">No Model Data</td>
</tr>
}
else
{
@foreach (var list in Model)
{
<tr>
<td>@Html.DisplayFor(Model => list.EmployeeID)</td>
<td>@Html.DisplayFor(Model => list.EmployeeName)</td>
<td>@Html.DisplayFor(Model => list.Address)</td>
<td>@Html.DisplayFor(Model => list.Email)</td>
<td>@Html.DisplayFor(Model => list.ContactNo)</td>
<td>@Html.DisplayFor(Model => list.Gender)</td>
<td>@Html.DisplayFor(Model => list.Birthday)</td>
<td>@Html.DisplayFor(Model => list.Status)</td>
<td>@Html.DisplayFor(Model => list.Position)</td>
<td>@Html.DisplayFor(Model => list.Department)</td>
<td>
<button class="btn-default" asp-action="Update" asp-route-id="@list.EmployeeID">Edit</button>
</td>
<td>
<form asp-action="Delete" method="post" asp-route-id="@list.EmployeeID">
<button class="btn-danger">Delete</button>
</form>
</td>
</tr>
}
}
</tbody>
Solution 1:[1]
Employee.cs
public class Employee
{
public string EmployeeID { get; set; }
public string EmployeeName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string ContactNo { get; set; }
public string Gender { get; set; }
public DateTime? Birthday { get; set; }
public string Status { get; set; }
public string Position { get; set; }
public string Department { get; set; }
}
HomeController.cs
public class HomeController : Controller
{
public IActionResult Employees()
{
var list = new List<Employee>
{
new Employee
{
EmployeeID = "EmployeeID",
Address = "Address",
Birthday = DateTime.Now,
ContactNo = "ContactNo",
Department = "Department",
Email = "Email",
EmployeeName = "EmployeeName",
Gender = "Gender",
Position = "Position",
Status = "Status"
},
new Employee
{
EmployeeID = "EmployeeID2",
Address = "Address",
Birthday = DateTime.Now,
ContactNo = "ContactNo",
Department = "Department",
Email = "Email",
EmployeeName = "EmployeeName",
Gender = "Gender",
Position = "Position",
Status = "Status"
}
};
return View(list);
}
}
Employees.cshtml
@model IEnumerable<WebApplication6.Controllers.Employee>
@{
ViewData["Title"] = "Employees";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h1>Employees</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.EmployeeID)
</th>
<th>
@Html.DisplayNameFor(model => model.EmployeeName)
</th>
<th>
@Html.DisplayNameFor(model => model.Address)
</th>
<th>
@Html.DisplayNameFor(model => model.Email)
</th>
<th>
@Html.DisplayNameFor(model => model.ContactNo)
</th>
<th>
@Html.DisplayNameFor(model => model.Gender)
</th>
<th>
@Html.DisplayNameFor(model => model.Birthday)
</th>
<th>
@Html.DisplayNameFor(model => model.Status)
</th>
<th>
@Html.DisplayNameFor(model => model.Position)
</th>
<th>
@Html.DisplayNameFor(model => model.Department)
</th>
<th></th>
</tr>
</thead>
<tbody>
@if (Model == null)
{
<tr>
<td colspan="7" class="text-center">No Model Data</td>
</tr>
}
else
{
foreach (var list in Model)
{
<tr>
<td>@Html.DisplayFor(Model => list.EmployeeID)</td>
<td>@Html.DisplayFor(Model => list.EmployeeName)</td>
<td>@Html.DisplayFor(Model => list.Address)</td>
<td>@Html.DisplayFor(Model => list.Email)</td>
<td>@Html.DisplayFor(Model => list.ContactNo)</td>
<td>@Html.DisplayFor(Model => list.Gender)</td>
<td>@Html.DisplayFor(Model => list.Birthday)</td>
<td>@Html.DisplayFor(Model => list.Status)</td>
<td>@Html.DisplayFor(Model => list.Position)</td>
<td>@Html.DisplayFor(Model => list.Department)</td>
<td>
<button class="btn-default" asp-action="Update" asp-route-id="@list.EmployeeID">Edit</button>
</td>
<td>
<form asp-action="Delete" method="post" asp-route-id="@list.EmployeeID">
<button class="btn-danger">Delete</button>
</form>
</td>
</tr>
}
}
</tbody>
</table>
It all works for me. Result image: /home/employees
Solution 2:[2]
What does your data look like if you select them in database?
Solution 3:[3]
I experienced what I believe was a similar issue.
I believe the cause of the problem is how Visual Studio was reading the definition of the SQL Server view -- specifically the combination of columns that were identified as "Entity Keys". I think Visual Studio simply finds all columns that are NOT NULL and sets them as Entity Keys.
In my case, I had a fairly complex SQL Server view (with multiple CTEs, a CROSS APPLY join, an EXCEPT sub-query, a UNION, and a couple LEFT OUTER JOINS). This complex SQL Server view does return accurate data. However, in the SQL Server view definition, some columns were defined as NULL when (logically) they will never actually be NULL. I had to force the SQL Server view column definitions to be NOT NULL. I had to use the SQL IsNull function. For example: SELECT ISNULL(col1,'') AS col1...
I then dropped and re-added the SQL Server view to the Visual Studio entity framework model and verified the proper columns were marked as entity keys.
This fixed my problem.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Levchenkov |
| Solution 2 | Jackal |
| Solution 3 | Dharman |
