编辑和删除数据库中的特定用户

时间:2014-09-26 09:15:40

标签: c# asp.net entity-framework

正如标题所说。我正在尝试为管理员启用删除和编辑系统。

管理员指定与数据库中的数据匹配的日期时间和用户ID,并显示给出的选择。

现在我需要知道如何编辑或删除特定数据。

型号:

public partial class Stamping
{
    [Key]
    [Column(Order = 0)]
    [DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int UserId { get; set; }

    [Key]
    [Column(Order = 1)]
    [DataType(DataType.DateTime)]
    public DateTime Timestamp { get; set; }

    [Required]
    [StringLength(3)]
    public string StampingType { get; set; }

    public virtual User User { get; set; }
}

查看:

@model IEnumerable<Aviato.Models.Stamping>
        @Html.DisplayNameFor(model => model.UserId)
        @Html.DisplayNameFor(model => model.Timestamp)
        @Html.DisplayNameFor(model => model.StampingType) @foreach (var item in Model) {
        @Html.DisplayFor(modelItem => item.UserId)
        @Html.DisplayFor(modelItem => item.Timestamp)
        @Html.DisplayFor(modelItem => item.StampingType)

        @Html.ActionLink("Edit", "Edit", new {  id=item.UserId  }) |
        @Html.ActionLink("Delete", "Delete", new {  id=item.UserId  })}

控制器:

    [AuthenticateRoles(Roles = "admin")]
public class StampingsController : Controller
{
    private AviatoModel db = new AviatoModel(); //Database Connection.

    public ActionResult Index(Stamping model)
    {
        var startTime = model.Timestamp;
        var endTime = model.Timestamp.AddDays(1);

        var stampings = db.Stampings.Where(s => s.Timestamp >= startTime && s.Timestamp <= endTime)
            .Where(s => s.UserId == model.UserId).ToList();

        return View(stampings);
    }

        public ActionResult Edit(int? id)
    {
        var stamping = db.Stampings.Find(id); /*An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code. Additional information: The number of primary key values passed must match number of primary key values defined on the entity.*/

        ViewBag.UserId = new SelectList(db.Users, "UserId", "SocialSecurityNumber", stamping.UserId);
        return View(stamping);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "UserId,Timestamp,StampingType")] Stamping stamping)
    {
        if (ModelState.IsValid)
        {
            db.Entry(stamping).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        ViewBag.UserId = new SelectList(db.Users, "UserId", "SocialSecurityNumber", stamping.UserId);
        return View(stamping);
    }

        public ActionResult Delete(int? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        Stamping stamping = db.Stampings.Find(id); //Same error as in Edit ActionResult.
        if (stamping == null)
        {
            return HttpNotFound();
        }
        return View(stamping);
    }

    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public ActionResult DeleteConfirmed(int id)
    {
        Stamping stamping = db.Stampings.Find(id);
        db.Stampings.Remove(stamping);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

我该如何解决这个问题?我试过围绕错误谷歌搜索,但没有成功。

所有帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您的实体具有由UserIdTimestamp组成的复合主键,但您只将一个值传递给Find()方法。该方法需要为主键中指定的每列提供一个值。

您应该将密钥修复为仅在一个列上(从Key中删除Timestamp属性)或传入两个值:

public ActionResult Edit(int? id, DateTime timestamp)
{
    var stamping = db.Stampings.Find(id, timestamp);
    //snip
}

不要忘记改变观点:

@Html.ActionLink("Edit", "Edit", new {  id=item.UserId, timestamp = item.Timestamp })