& # 39; HttpPostedFileBase& # 39;没有定义键
本文关键字:定义 HttpPostedFileBase | 更新日期: 2023-09-27 18:13:59
我有一个web应用程序上传图像到数据库并检索它们。
public class ImageGallery
{
[Key]
public int ImageID { get; set; }
public int ImageSize { get; set; }
public string FileName { get; set; }
public byte[] ImageData { get; set; }
[Required(ErrorMessage="Please select Image File")]
public HttpPostedFileBase file { get; set; }
}
我的数据库上下文类是这样的
public class MyDatabaseEntities : DbContext
{
public DbSet<ImageGallery> ImageGalleries { get; set; }
}
这里是控制器
public ActionResult Upload()
{
return View();
}
[HttpPost]
public ActionResult Upload(ImageGallery IG)
{
IG.FileName = IG.File.FileName;
IG.ImageSize = IG.File.ContentLength;
byte[] data = new byte[IG.File.ContentLength];
IG.File.InputStream.Read(data, 0, IG.File.ContentLength);
IG.ImageData = data;
using(MyDatabaseEntities dc = new MyDatabaseEntities())
{
dc.ImageGalleries.Add(IG);
dc.SaveChanges();
}
return RedirectToAction("Gallery");
}
现在当我尝试上传图像时它会给我以下错误
EntityType ' httppostdfilebase '没有定义键。定义这个EntityType的键。HttpPostedFileBase: EntityType: EntitySet 'HttpPostedFileBase'基于类型'HttpPostedFileBase',没有定义键。
我看到一个关于堆栈溢出的问题-----'HttpPostedFileBase'没有定义键。定义这个EntityType的键。我尝试了这个解决方法,但没有成功。
我关注这个博客是为了这个目的------http://dotnetawesome.com/mvc/how-to-upload-image-to-database-and-show-in-view-without-image-handler
试试这个
[NotMapped]
public HttpPostedFileBase File { get; set; }
这不会映射到数据库。
出现错误是因为表中没有数据类型HttpPostedFileBase
不能将HttpPostedFileBase
存储在数据库字段中(它是包含多个属性的复杂对象)。您可以使用[NotMapped]
属性排除这一点,但是您的模型和视图确实没有关系(您不包括模型的其他属性的任何输入)。
相反,你的视图可以只是
@using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="file" />
<input type = "submit" value="Upload" />
}
并将POST方法更改为
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
if (file.ContentLength > 0) // check a file was selected
{
// Initialize a new instance of the data model and set its properties
ImageGallery model = new ImageGallery()
{
FileName = file.FileName,
ImageSize = file.ContentLength,
....
};
.... // save and redirect
}
else {
// add a model state error and return the view?
}
}
尝试使用如下属性创建一个新的视图模型-----
public class ImageViewModel
{
public int ImageID { get; set; }
public int ImageSize { get; set; }
public string FileName { get; set; }
public byte[] ImageData { get; set; }
public HttpPostedFileBase File { get; set; }
}
并像这样改变你的控制器-----
public ActionResult Upload(HttpPostedFileBase file)
{
ImageGallery IG = new ImageGallery();
IG.FileName = file.FileName;
IG.ImageSize = file.ContentLength;
byte[] data = new byte[file.ContentLength];
file.InputStream.Read(data, 0, file.ContentLength);
IG.ImageData = data;
var model = new ImageViewModel
{
FileName = file.FileName,
ImageSize = file.ContentLength,
ImageData = data,
File = file
};
using(MyDatabaseEntities dc = new MyDatabaseEntities())
{
dc.ImageGalleries.Add(IG);
dc.SaveChanges();
}
return View(model);
}
}
}
并像这样更改视图页面------
@model Image.Models.ImageViewModel
<h2>Upload</h2>
@using (Html.BeginForm("Upload", "ImageGallery", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.ValidationSummary(true)
<table>
<tr>
<td>Select File : </td>
<td>
@Html.TextBoxFor(Model => Model.File, new { type = "file" })
@Html.ValidationMessage("CustomError")
</td>
<td>
<input type="submit" value="Upload" />
</td>
</tr>
</table>
}