我在這裏遇到問題。當我從我的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>
當你刪除上述它調用相同的'行動'方法? – jamiedanq
是的,它調用創建動作 – prezequias
使用保存錯誤的對象 – jamiedanq