我是一个使用实体框架的ASP.NET新手。我有不同的模型,包括People、FileType和FilePath。我想通过从FilPath检索文件路径并在索引视图中显示像名称、年龄等数据来显示图像。我已经在详细视图中实现了这一点,但在索引视图页中,我收到了“值不能为空”的错误,这是由于PeopleDB中的FilePath为空造成的。
以下是我的代码,请帮忙。谢谢。
感谢您的帮助。如果您需要更多的代码,请让我知道。
以下是我的代码,请帮忙。谢谢。
/Model/PeopleDB.cs
namespace MvcDemo.Models {
public class PeopleDB
{
public PeopleDB()
{
this.FilePaths = new HashSet<FilePath>();
}
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
public string Address { get; set; }
public string Interests { get; set; }
public ICollection<FilePath> FilePaths { get; set; }
}
public class PeopleDBContext : DbContext
{
public DbSet<FilePath> FilePaths { get; set; }
public DbSet<PeopleDB> People { get; set; }
}
}
/Model/FilePath.cs
namespace Assessment_HC.Models
{
public class FilePath
{
public int FilePathId {get;set;}
[StringLength(255)]
public string FileName {get;set;}
public FileType FileType {get;set;}
public int PersonID {get;set;}
public virtual PeopleDB Person {get;set;}
}
}
Moedel/FileType.cs
namespace Assessment_HC.Models
{
public enum FileType
{
Avatar = 1, Photo
}
}
这里是用于索引视图的控制器
//Get: /People/Index
public ActionResult Index()
{
return View(db.People.ToList());
}
在db.People.ToList()中,People.FilePath视图为空。
在控制器中,详细视图如下,我可以从中获取在详细页面上显示的图像:
// GET: /People/Details
public ActionResult Details(int id = 0)
{
PeopleDB peopledb = db.People.Find(id);
PeopleDB people = db.People.Include(i => i.FilePaths).SingleOrDefault(i => i.ID == id);
if (peopledb == null)
{
return HttpNotFound();
}
return View(peopledb);
}
感谢您的帮助。如果您需要更多的代码,请让我知道。