2017-09-27 39 views
1

我有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; } 
} 
+0

開始刪除'new {name =「file1」}'等,這絕對不是NG。並且你的POST方法的簽名應該有一個參數,它是你的模型,以便這3個'HttpPostedFileBase'屬性綁定在你的模型中 –

+0

但是你註釋掉代碼表明這3個屬性是'string',在這種情況下你不能綁定一個文件輸入到一個「字符串」(你需要綁定到屬性是'HttpPostedFileBase') –

+0

我只想將每個文件名保存到不同的列名。我已經完成了1個文件的工作,但我不確定如何區分不同的文件 – user7221204

回答

1

您可以將文件輸入不綁定到string財產。首先創建一個包含在視圖中所需的屬性視圖模型(總是使用view model編輯時的數據))

public class ModelNameVM 
{ 
    public int? PinNumbers { get; set; } 
    public HttpPostedFileBase SerialAttachment { get; set; } 
    public HttpPostedFileBase CountryAttachment { get; set; } 
    public HttpPostedFileBase OtherAttachment { get; set; } 
} 

,並添加顯示和驗證屬性作爲適當

你的看法屆時將

@model ModelNameVM 
.... 
@using (Html.BeginForm()) 
{ 
    .... 
    @Html.TextBoxFor(m => m.SerialAttachment, new { type = "file" }) 
    @Html.TextBoxFor(m => m.CountryAttachment , new { type = "file" }) 
    .... 

請注意,使用new { name = "..." }什麼也不做,並且不需要覆蓋由HtmlHelper方法創建的默認id屬性。

在POST方法中的代碼後會有

public ActionResult Create(ModelNameVM model) 
{ 
    if (!ModelState.IsValid) 
    { 
     return View(model); 
    } 
    // Initialize a new instance of your data model and map values from the view model 
    ModelName data = new ModelName 
    { 
     PinNumbers = model.PinNumbers 
    }; 
    if (model.SerialAttachment != null && model.SerialAttachment.ContentLength > 0) 
    { 
     string fileName = Path.GetFileName(model.SerialAttachment.FileName); 
     ..... 
     model.SerialAttachment.SaveAs(path); 
     data.SerialAttachment = path 
    } 
    .... // repeat for CountryAttachment and OtherAttachment 
    db.model.Add(data); 
    db.SaveChanges(); 
    return RedirectToAction("Index"); 
} 

注意事項使用視圖模型,當你不需要[Bind]屬性(您已經受到保護,免受過張貼攻擊。

此外,你不是映射到你的應用程序中的文件夾上傳文件,你需要類似的東西

var path = Path.Combine(Server.MapPath("~/Images"), fileName); 
+0

謝謝,我想我明白了。我正在嘗試它,並會很快回復您。 – user7221204

+0

工作完美!謝謝你這麼清楚的解釋! – user7221204

+0

如果我要將一個自動編號字段(即「Id」)附加到文件名並將其分配給路徑,我將如何在插入之前獲取id值。如下所示:var path = Path.Combine(Networkpath,data.Id + fileName);我得到的ID爲0,因爲我想使用尚未被插入數據庫的ID。 – user7221204

相關問題