我有3個表單輸入3個文件,我將不得不將它們保存到數據庫(sqlserver)中的不同列中。我無法弄清楚如何將每個文件名添加到單獨的列。請幫忙。我試圖在我的控制器中實現註釋掉的代碼,但我不確定語法是什麼。我的代碼如下:Mvc razorview上傳3個文件並將文件名保存到數據庫中的3個不同列
查看:
@Html.TextBoxFor(model => model.SerialAttachment, new { type = "file", name = "file1", id="file1" })
@Html.TextBoxFor(model => model.CountryAttachment, new { type = "file", name = "file2", id = "file2" })
@Html.TextBoxFor(model => model.OtherAttachment, new { type = "file", name = "file3", id = "file3" })
控制器:
`public ActionResult Create([Bind(Include = "Id,Serial,PinNumbers,SerialAttachment,CountryAttachment,OtherAttachment")] ModelName modelInstance`) {
if (Request.Files.Count > 0)
{
for (int i = 0; i < Request.Files.Count; i++)
{
var fileUp = Request.Files[i];
if (fileUp != null && fileUp.ContentLength > 0)
{
var fname = Path.GetFileName(fileUp.FileName);
var path = Path.Combine(Server.MapPath(fname));
fileUp.SaveAs(path);
// modelInstance.SerialAttachment = fname;
// modelInstance.CountryAttachment = fname;
// modelInstance.OtherAttachment = fname;
db.model.Add(modelInstance);
db.SaveChanges();
}
}
return RedirectToAction("Index");
}
型號:
public partial class ModelName
{
public int? Serial { get; set; }
public int? PinNumbers { get; set; }
[StringLength(250)]
public string SerialAttachment { get; set; }
[StringLength(250)]
public string CountryAttachment { get; set; }
[StringLength(250)]
public string OtherAttachment { get; set; }
}
開始刪除'new {name =「file1」}'等,這絕對不是NG。並且你的POST方法的簽名應該有一個參數,它是你的模型,以便這3個'HttpPostedFileBase'屬性綁定在你的模型中 –
但是你註釋掉代碼表明這3個屬性是'string',在這種情況下你不能綁定一個文件輸入到一個「字符串」(你需要綁定到屬性是'HttpPostedFileBase') –
我只想將每個文件名保存到不同的列名。我已經完成了1個文件的工作,但我不確定如何區分不同的文件 – user7221204