我这里有问题。当我从我的BeginForm中删除(“Creae”,“DriverRegs”,FormMethod.Post,new {enctype =“multipart / form-data”})时,图像路径通常存储在数据库中,但是当我把上面的内容放回到BeginForm,然后图像文件存储在我的文件夹中,但是在数据库上出现此错误 System.Web.HttpPostedFileWrapper 。这些代码有什么问题? 如果有人可以帮助我,我会感激。 提前谢谢。
这是我的代码:
CONTROLLER
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);
}
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; }
}
查看
@using(Html.BeginForm(“Create”,“DriverRegs”,FormMethod.Post,new {enctype =“multipart / form-data”})) { @ 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 :(得分:1)
这是一个让你烦恼的小细节,所以按照评论
进行更改 db.DriverRegs.Add(newRecord);//change this part
db.SaveChanges();
修改强>
首先,我不知道当你删除它时它是如何工作的,但是你要保存的不是DriveReg model
,因为这就是你正在做的事情。相反,您想要保存DriverReg newRecord
当表单发布到控制器时,它会将文件发布为 System.Web.HttpPostedFileWrapper
,因此如果您保存DriverReg model
,那就是您在数据库。那时你还没有完成一些逻辑来获得你phisicalPath
所展示的DriverReg newRecord
。由于newRecord
和model
属于同一类型DriverReg
,因此您看起来好像保存了正确的数据,但事实并非如此。