Ahmed Aboumalek
Ahmed Aboumalek

Reputation: 623

ASP.NET MVC 3 - getting duplicate Rows

I'm currently using MVC 3 with Entity Framework 5. So this is my controller that is called MachineController:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gestion_Machines.Models;

namespace Gestion_Machines.Controllers
{ 
    public class MachineController : Controller
    {
        private RTTV5Entities db = new RTTV5Entities();

        //
        // GET: /Machine/

        public ViewResult Index()
        {
            return View(db.tMachines.ToList());
        }

        //
        // GET: /Machine/Details/5

        public ViewResult Details(string id)
        {
            tMachines tmachines = db.tMachines.Find(id);
            return View(tmachines);
        }

        //
        // GET: /Machine/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Machine/Create

        [HttpPost]
        public ActionResult Create(tMachines tmachines)
        {
            if (ModelState.IsValid)
            {
                db.tMachines.Add(tmachines);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(tmachines);
        }

        //
        // GET: /Machine/Edit/5

        public ActionResult Edit(string id)
        {
            tMachines tmachines = db.tMachines.Find(id);
            return View(tmachines);
        }

        //
        // POST: /Machine/Edit/5

        [HttpPost]
        public ActionResult Edit(tMachines tmachines)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tmachines).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(tmachines);
        }

        //
        // GET: /Machine/Delete/5

        public ActionResult Delete(string id)
        {
            tMachines tmachines = db.tMachines.Find(id);
            return View(tmachines);
        }

        //
        // POST: /Machine/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(string id)
        {            
            tMachines tmachines = db.tMachines.Find(id);
            db.tMachines.Remove(tmachines);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}

This is my Index View that was automatically generated after creating the controller along with create, delete, details and Edit:

@model IEnumerable<Gestion_Machines.Models.tMachines>

@{
    ViewBag.Title = "Index";
}

<link href="../../Content/machine.css" rel="stylesheet" />
<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            NomMachine
        </th>
        <th>
            Fabriquant
        </th>
        <th>
            MachineSection
        </th>
        <th>
            TypeMachine
        </th>
        <th>
            VitesseMaxi
        </th>
        <th>
            NbPistes
        </th>
        <th>
            UniteVitesse
        </th>
        <th>
            UniteEntrante
        </th>
        <th>
            UniteSortante
        </th>
        <th>
            Efficience
        </th>
        <th>
            EfficienceGlobale
        </th>
        <th>
            VitesseMoy
        </th>
        <th>
            TauxPanne
        </th>
        <th>
            TauxArret
        </th>
        <th>
            RunM2
        </th>
        <th>
            RunML
        </th>
        <th>
            TCO
        </th>
        <th>
            Section
        </th>
        <th>
            Atelier
        </th>
        <th>
            TransfertFocus
        </th>
        <th>
            MagasinEntree
        </th>
        <th>
            MagasinSortie
        </th>
        <th>
            NbPalettes
        </th>
        <th>
            MagasinSortieWIP
        </th>
        <th>
            MagasinSortieSF
        </th>
        <th>
            MagasinSortieFG
        </th>
        <th>
            Qualite
        </th>
        <th>
            BlistersPerCarton
        </th>
        <th>
            SeuilProdAtteint
        </th>
        <th>
            TpsChgmtOf
        </th>
        <th>
            QteProdSaisie
        </th>
        <th>
            Active
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.NomMachine)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Fabriquant)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MachineSection)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TypeMachine)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VitesseMaxi)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NbPistes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UniteVitesse)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UniteEntrante)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.UniteSortante)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Efficience)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.EfficienceGlobale)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.VitesseMoy)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TauxPanne)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TauxArret)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RunM2)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.RunML)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TCO)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Section)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Atelier)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TransfertFocus)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MagasinEntree)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MagasinSortie)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.NbPalettes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MagasinSortieWIP)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MagasinSortieSF)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MagasinSortieFG)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Qualite)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.BlistersPerCarton)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SeuilProdAtteint)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TpsChgmtOf)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.QteProdSaisie)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Active)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
            @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
        </td>
    </tr>
}

</table>

So my problem is when I access /Machine/Index I do get the appropriate table, but somehow It contains duplicated records, while other records are missing. I haven't changed anything in the controller or the View, both of them where generated.

ps: my database doesn't contain any duplicated ones.

edit: I can see exactly 49 records on my table view. Basically only 3 different records where one is repeated 6 times, another is repeated 42 times and another not repeated. On my original database table, I got exactly 49 records where there are no duplicates.

Having tried to view my Machines table from the server explorer (right click > "Show Table Data"), I got the following error: This Database Cannot be imported. It is either an unsupported SQL Server version or an unsupported database compatibility.

I'm currently searching for this error.

Upvotes: 1

Views: 1740

Answers (1)

&#192;ndres
&#192;ndres

Reputation: 121

I actually had this problem a few weeks ago, it was driving me crazy until I decided to update my Visual Studio, go to tools > Extensions and Updates > Updates , make sure that your VS is updated to the last version This solved the problem for me.

Hope I helped

Upvotes: 1

Related Questions