C# 编辑和删除数据库中的特定用户
正如标题所说。我正在尝试为管理员启用删除和编辑系统 管理员指定与数据库中的数据匹配的日期时间和用户ID,并显示给定的选项 现在我需要知道如何编辑或删除特定数据 型号:C# 编辑和删除数据库中的特定用户,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,正如标题所说。我正在尝试为管理员启用删除和编辑系统 管理员指定与数据库中的数据匹配的日期时间和用户ID,并显示给定的选项 现在我需要知道如何编辑或删除特定数据 型号: public partial class Stamping { [Key] [Column(Order = 0)] [DatabaseGenerated(DatabaseGeneratedOption.None)] public int UserId { get; set; } [Key]
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; }
}
视图:
控制器:
[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");
}
我该如何解决这个问题?伊夫尝试用谷歌搜索错误,但没有成功
所有的帮助将不胜感激 您的实体有一个由UserId和Timestamp组成的复合主键,但您只将一个值传递给Find方法。该方法要求主键中指定的每一列都有一个值 您应该将键固定在单个列上,从时间戳中删除键属性,或者传入两个值:
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 })
您遇到的错误是什么?EntityFramework.dll中出现“System.ArgumentException”类型的异常,但未在用户代码中处理。附加信息:传递的主键值的数量必须与实体上定义的主键值的数量匹配。是的,在这两个方面…确保在问题本身中包含相关的异常消息和堆栈跟踪或其他不起作用的指示器。我实现了它并得到:System.ArgumentException:parameters dictionary包含不可为null的类型“System.DateTime”for method“System.Web.Mvc.ActionResult”的参数“timeStamp”的null条目“Aviato.Controllers.StampingsController”中的EditSystem.Nullable`1[System.Int32],System.DateTime'。可选参数必须是引用类型、可为null的类型或声明为可选参数。参数名称:参数现在的问题是如何保存数据,当我中断HttpPost ActionResult时,UserId和StampingType为null。。。
@Html.ActionLink("Edit", "Edit", new { id=item.UserId, timestamp = item.Timestamp })