2016-04-24 75 views
0

我在這裏遇到問題。當我從我的BeginForm中刪除(「Creae」,「DriverRegs」,FormMethod.Post,new {enctype =「multipart/form-data」})時,圖像路徑通常存儲在數據庫中,但是當我把它上面回到BeginForm,然後圖像文件存儲在我的文件夾,但在數據庫上出現這個錯誤System.Web.HttpPostedFileWrapper。代碼有什麼問題? 我會apprreciate如果有人可以幫助我。 在此先感謝。System.Web.HttpPostedFileWrapper在MVC C中將圖像上傳到數據庫時出錯#

這裏是我的代碼:

控制器

public ActionResult Create(DriverReg model, HttpPostedFileBase file) 
     { 
      if (ModelState.IsValid) 
      { 
       if (file != null && file.ContentLength > 0) 
       { 
        var fileName = Path.GetFileName(file.FileName); 
        var phisicalPath = Path.Combine(Server.MapPath("~/Content/uploads/"), fileName); 
        file.SaveAs(phisicalPath); 
        DriverReg newRecord = new DriverReg(); 
        newRecord.FullName = model.FullName; 
        newRecord.Address = model.Address; 
        newRecord.Postcode = model.Postcode; 
        newRecord.Contact = model.Contact; 
        newRecord.Email = model.Email; 
        newRecord.County = model.County; 
        newRecord.File = phisicalPath; 
       } 
       db.DriverRegs.Add(model); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(model); 
     } 

模型

public class DriverReg 
    { 
     [Key] 
     public int DrvId { get; set; } 
     public string FullName { get; set; } 
     public string Address { get; set; } 
     public string Postcode { get; set; } 
     public string Contact { get; set; } 
     public string Email { get; set; } 
     public string County { get; set; } 
     [DataType(DataType.Upload)] 
     [Display(Name = "Upload Files")] 
     [Required(ErrorMessage = "Please choose file to upload")] 
     public string File { get; set; } 
     public DateTime Date { get; set; } 


     internal int UploadImageInDataBase(HttpPostedFileBase file, DriverRegViewModel model) 
     { 
      throw new NotImplementedException(); 
     } 

    } 
    public class DriverDbContext : DbContext 
    { 
     public DriverDbContext() 
      : base("VanRemovals") 
     { 
     } 

    public static DriverDbContext Create() 
     { 
      return new DriverDbContext(); 
     } 

      public DbSet<DriverReg> DriverRegs { get; set; } 
    } 

VIEW

@using(Html.BeginForm( 「創建」, 「DriverRegs」,FormMethod.Post,新的{ENCTYPE = 「多部分/格式數據」})) { @ Html.AntiForgeryToken()

<div class="form-horizontal"> 
     <h4>DriverReg</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.FullName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.FullName, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.FullName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Postcode, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Postcode, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Postcode, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Contact, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Contact, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Contact, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.County, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.County, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.County, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.File, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       <input type="file" class="file-input" name="file" /> 
       @Html.ValidationMessageFor(model => model.File, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

+0

當你刪除上述它調用相同的'行動'方法? – jamiedanq

+0

是的,它調用創建動作 – prezequias

+1

使用保存錯誤的對象 – jamiedanq

回答

1

這是與你搞亂了微小的細節,所以要改變與評論指出

db.DriverRegs.Add(newRecord);//change this part 
       db.SaveChanges(); 

編輯

首先,我不知道它是如何工作的很好,當你刪除你的位,但然後你想保存的不是DriveReg model,因爲這就是你在做什麼。相反,你要保存DriverReg newRecord

當表單發送到控制器其職位文件System.Web.HttpPostedFileWrapper所以如果你保存DriverReg model這就是你必須在你的數據庫中。在那時,你還沒有完成一點邏輯來獲得phisicalPath,因爲你在DriverReg newRecord中顯示你想要的。由於newRecordmodel都是相同類型的DriverReg,因此它看起來好像保存了正確的數據,但您不是。

+0

感謝您的幫助,排序問題。 – prezequias