ASP.NET MVC 3 - 获取重复的行

时间:2016-03-30 12:23:23

标签: asp.net-mvc-3

我目前正在使用MVC 3和Entity Framework 5。 所以这是我的控制器,叫做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);
        }
    }
}

这是我在创建控制器后自动生成的索引视图以及创建,删除,详细信息和编辑:

@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>

所以我的问题是当我访问/Machine/Index时,我确实得到了相应的表,但不知怎的,它包含重复的记录,而其他记录则丢失了。 我没有更改控制器或视图中的任何内容,它们都是生成的。

ps:我的数据库不包含任何重复的数据库。

编辑: 我可以在桌面视图中看到准确的49条记录。基本上只有3个不同的记录,其中一个重复6次,另一个重复42次而另一次不重复。 在我的原始数据库表中,我得到了49条记录,其中没有重复项。

尝试从服务器资源管理器中查看我的Machines表(右键单击&gt;“显示表数据”),我收到以下错误: This Database Cannot be imported. It is either an unsupported SQL Server version or an unsupported database compatibility.

我正在搜索此错误。

1 个答案:

答案 0 :(得分:1)

几周前我确实遇到过这个问题,在我决定更新我的Visual Studio之前,它让我发疯,转到from pandas.stats.api import ols grps = df.groupby(['FID']) for fid, grp in grps: result = ols(y=grp.loc[:, 'MEAN'], x=grp.loc[:, ['Accum_Prcp', 'Accum_HDD']]) text_file = open("Output {}.txt".format(fid), "w") text_file.write(result.summary) text_file.close() ,确保你的VS已更新到最新版本 这解决了我的问题。

希望我帮助