【发布时间】:2012-11-20 07:44:50
【问题描述】:
我有一些疑问,也许是新手的疑问,但我刚刚进入 ASP.NET MVC 4 基本上我想知道在模型中获取对象细节的正确方法。 在这种情况下,承包商内部的图像。
型号:
public class Contractor {
[Key]
public int ContractorID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Address { get; set; }
public Image Avatar { get; set; }
}
public class Image {
[Key]
public int ImageID { get; set; }
[Required]
public string File_name { get; set; }
[Required]
public byte[] File_data { get; set; }
}
public class DATACRUD : DbContext {
public DbSet<Contractor> Companies { get; set; }
public DbSet<Image> Images { get; set; }
}
控制器:
private DATACRUD db = new DATACRUD();
public ActionResult GetContractorAvatar(int id)
{
Contractor contractor = db.Contractors.Find(id);
if (contractor == null)
{
return HttpNotFound();
}
Image avatar = contractor.Avatar;
问题 1)
avatar == null,但不应该是因为当我创建对象 Contractor 时,我成功添加了图像(我检查了数据库,它就在那里)
我看到的解决方案不是在 Contractor.cs 模型中使用 Image 属性,而是将字符串属性与 image 键放在一起。
问题 2)
即使可以像我在上一个问题中所说的那样抓住图像键,当我在调试模式下通过鼠标时 私有 DATACRUD db = 新 DATACRUD ();
db.Images 也是空的...
return File(avatar.File_data, "image");
}
【问题讨论】:
标签: database image object model asp.net-mvc-4