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

本文关键字:用户 删除 数据库 编辑 | 更新日期: 2023-09-27 18:24:31

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

管理员指定与数据库中的数据匹配的日期时间和用户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");
    }

我该如何解决这个问题?Ive尝试在谷歌上搜索错误,但没有成功。

我们将不胜感激!

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

您的实体有一个由UserIdTimestamp组成的复合主键,但您只向Find()方法传递了一个值。该方法要求主键中指定的每列都有一个值。

您应该将键固定为仅在一列上(从Timestamp中删除Key属性),或者传入两个值:

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 })