在 MVC 4 中创建 ImagesController 时出现错误
我正在尝试在 MVC4 中创建一个像这样的 ImagesController
但我不断收到此错误。
使用此类为 PeopleController 创建控制器没有问题
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual IEnumerable<Affair> Affairs { get; set; }
}
I'm trying to create an ImagesController in MVC4 like this
But I keep getting this error.
Had no problem creating controller for PeopleController using this class
public class Person
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual IEnumerable<Affair> Affairs { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题出在
Image
类上的File
属性。因为 EntityFramework 无法理解类型HttpPostedFileBase
并且无法将其存储在数据库中,并且控制器生成器足够智能来检查这一点。尽管错误消息并没有告诉您问题是什么。要解决此问题,您应该重写属性以使用字节数组:然后控制器生成应该可以工作。您可以添加自己的图像上传操作,如下所示:
The problem is with your
File
property on theImage
class. Because EntityFramework won't understand the typeHttpPostedFileBase
and can't store it in the DB and the controller generator is smart enough to check this. Altough the error message doesn't tell you what is the problem. To fix this you should rewrite your property to use a byte array:And then the controller generation should work. And you can add your own image upload action, something like this: