如何将 "upload file button" 添加到我现有的表单中,并在 UploadFile 数据库的新 table 中获取先前表单的主键?
How to add "upload file button" with my existing form and get the primary key of that previous form in the new table of UploadFile Database?
这是我的控制器,其中已存在以下字段:
[HttpPost]
public ActionResult Save(Rent Rent)
{
if (Rent.Id == 0)
_Context.Rent.Add(Rent);
else
{
var rentInDb = _Context.Rent.Single(c => c.Id == Rent.Id);
rentInDb.tenantId = Rent.tenantId;
rentInDb.unitId = Rent.unitId;
rentInDb.startDate = Rent.startDate;
rentInDb.endDate = Rent.endDate;
rentInDb.Amount = Rent.Amount;
rentInDb.leaseStatus = Rent.leaseStatus;
}
_Context.SaveChanges();
return RedirectToAction("leaseStatus", "Home");
}
我已经创建了 uploadFile
table,我可以在其中获取它的上传路径,但我希望以前的控制器 ID 与每次上传的文件合并,并且希望以前的表单 ID 在我创建的 UploadFile
的新数据库!
PS:我首先使用代码在 uploadFile
table 中添加了以前的表单 ID。
我需要控制器的代码
好吧,基本上我希望我的表格看起来像这样:
I wanted the existing fields like tenantId , UnitId , StartDate ,etc from my previous database and then I created a new database for file Upload and made my form look like this !!
查询的是如何将我的旧数据库的 ID 获取到 "FileUpload" 的新数据库中。
这就是我的控制器和 Action 的样子:
[HttpPost]
public ActionResult Save(Rent Rent , FileUpload upload, HttpPostedFileBase file)
{
if (Rent.Id == 0)
_Context.Rent.Add(Rent);
else
{
var rentInDb = _Context.Rent.Single(c => c.Id == Rent.Id);
rentInDb.tenantId = Rent.tenantId;
rentInDb.unitId = Rent.unitId;
rentInDb.startDate = Rent.startDate;
rentInDb.endDate = Rent.endDate;
rentInDb.Amount = Rent.Amount;
rentInDb.leaseStatus = Rent.leaseStatus;
}
_Context.SaveChanges();
var rent = _Context.Rent.Single(r => r.Id == Rent.Id);
var up = Request.Files["file"];
if (up.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var guid = Guid.NewGuid().ToString();
var path = Path.Combine(Server.MapPath("~/uploads"), guid + fileName);
file.SaveAs(path);
string fl = path.Substring(path.LastIndexOf("\"));
string[] split = fl.Split('\');
string newpath = split[1];
string imagepath = "~/uploads/" + newpath;
upload.length = imagepath;
upload.Rent = rent;
_Context.FileUpload.Add(upload);
_Context.SaveChanges();
}
return RedirectToAction("leaseStatus", "Home");
}
在这个 POST 方法中,旧数据库的现有形式是在 _Context.SaveChanges();
的帮助下添加的
然后我从现有字段中获得的 ID 保存在我创建的新变量中就在 _Context.SaveChanges(); 之后
并在 fileUpload 代码中调用该值。
And Finally this is how I got my Id of old database of existing form into new Table of fileUpload !!
这是我的控制器,其中已存在以下字段:
[HttpPost]
public ActionResult Save(Rent Rent)
{
if (Rent.Id == 0)
_Context.Rent.Add(Rent);
else
{
var rentInDb = _Context.Rent.Single(c => c.Id == Rent.Id);
rentInDb.tenantId = Rent.tenantId;
rentInDb.unitId = Rent.unitId;
rentInDb.startDate = Rent.startDate;
rentInDb.endDate = Rent.endDate;
rentInDb.Amount = Rent.Amount;
rentInDb.leaseStatus = Rent.leaseStatus;
}
_Context.SaveChanges();
return RedirectToAction("leaseStatus", "Home");
}
我已经创建了 uploadFile
table,我可以在其中获取它的上传路径,但我希望以前的控制器 ID 与每次上传的文件合并,并且希望以前的表单 ID 在我创建的 UploadFile
的新数据库!
PS:我首先使用代码在 uploadFile
table 中添加了以前的表单 ID。
我需要控制器的代码
好吧,基本上我希望我的表格看起来像这样: I wanted the existing fields like tenantId , UnitId , StartDate ,etc from my previous database and then I created a new database for file Upload and made my form look like this !!
查询的是如何将我的旧数据库的 ID 获取到 "FileUpload" 的新数据库中。
这就是我的控制器和 Action 的样子:
[HttpPost]
public ActionResult Save(Rent Rent , FileUpload upload, HttpPostedFileBase file)
{
if (Rent.Id == 0)
_Context.Rent.Add(Rent);
else
{
var rentInDb = _Context.Rent.Single(c => c.Id == Rent.Id);
rentInDb.tenantId = Rent.tenantId;
rentInDb.unitId = Rent.unitId;
rentInDb.startDate = Rent.startDate;
rentInDb.endDate = Rent.endDate;
rentInDb.Amount = Rent.Amount;
rentInDb.leaseStatus = Rent.leaseStatus;
}
_Context.SaveChanges();
var rent = _Context.Rent.Single(r => r.Id == Rent.Id);
var up = Request.Files["file"];
if (up.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var guid = Guid.NewGuid().ToString();
var path = Path.Combine(Server.MapPath("~/uploads"), guid + fileName);
file.SaveAs(path);
string fl = path.Substring(path.LastIndexOf("\"));
string[] split = fl.Split('\');
string newpath = split[1];
string imagepath = "~/uploads/" + newpath;
upload.length = imagepath;
upload.Rent = rent;
_Context.FileUpload.Add(upload);
_Context.SaveChanges();
}
return RedirectToAction("leaseStatus", "Home");
}
在这个 POST 方法中,旧数据库的现有形式是在 _Context.SaveChanges();
的帮助下添加的然后我从现有字段中获得的 ID 保存在我创建的新变量中就在 _Context.SaveChanges(); 之后
并在 fileUpload 代码中调用该值。
And Finally this is how I got my Id of old database of existing form into new Table of fileUpload !!