如何编辑已存在的行
我正在给视频添加一行,然后我想使用与视频的物理文件名相同的ID。所以我需要添加没有文件名的行,然后使用我得到的ID,然后用文件名更新该行。我现在确定如何做到这一点。如何编辑已存在的行
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file) {
Video v = new Video();
v.Name = VideoName;
v.Report = Report;
db.Videos.Add(v);
var filename = v.ID + "." + Path.GetExtension(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
v.FileName = filename;
//** update Row here with filename
db.SaveChanges();
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}
回答:
假设您有一个自动递增数据库中ID的主键,您需要在引用ID属性之前调用保存更改。您必须将实体保存到数据库,以便可以分配ID。
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file) {
Video v = new Video();
v.Name = VideoName;
v.Report = Report;
db.Videos.Add(v);
db.SaveChanges();
var filename = v.ID + "." + Path.GetExtension(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
v.FileName = filename;
db.SaveChanges();
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}
由于“文件名”只是该ID的组合和文件扩展名,为什么不保存文件的扩展名并做串联,当你需要引用的视频。这会将您对数据库的呼叫降低到一个,并在DB存储上节省一点
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file) {
Video v = new Video();
v.Name = VideoName;
v.FileName = Path.GetExtension(file.FileName);
v.Report = Report;
db.Videos.Add(v);
db.SaveChanges();
var filename = v.ID + "." + v.FileName;
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}
以上是 如何编辑已存在的行 的全部内容, 来源链接: utcz.com/qa/259853.html