Asp.net MVC,显示来自数据库的数据(多对多)
Asp.net MVC , showing data from db(many to many)
我有 2 个 tables,书籍和属性,它们与 table Attribute_Book 之间存在多对多关系。我没有设法执行创建功能,只是想在详细信息视图中显示来自第 3 个 table 的数据。我手动放置它只是为了检查并遇到问题,除了我想显示的值之外,它还显示一个代理,它是属性名称(用断点看到它)。项目首先是数据库。感谢您的帮助。
这是我的 Books ViewModel
public BookModel()
{
this.Attribute_Book = new HashSet<Attribute_Book>();
}
public int BookID { get; set; }
public string Title { get; set; }
public int AuthorID { get; set; }
public int CountryID { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public Nullable<int> PagesCount { get; set; }
public string Picture { get; set; }
public decimal TotalPrice { get; set; }
public virtual ICollection<Attribute_Book> Attribute_Book { get; set; }
public virtual Author Author { get; set; }
public virtual Country Country { get; set; }
这是我的属性模型
public Attribute()
{
this.Attribute_Book = new HashSet<Attribute_Book>();
}
public int AttributeID { get; set; }
public string Name { get; set; }
public int AttTypeID { get; set; }
public virtual ICollection<Attribute_Book> Attribute_Book { get; set; }
public virtual Attribute_Type Attribute_Type { get; set; }
这是我的 Attribute_Book 模型
public int ID { get; set; }
public int BookID { get; set; }
public int AttributeID { get; set; }
public string ValueTypeText { get; set; }
public Nullable<System.DateTime> ValueTypeDate { get; set; }
public virtual Attribute Attribute { get; set; }
public virtual Book Book { get; set; }
这是我在控制器中的详细操作
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
//Book book = await db.Books.Include(b=>b.Attribute_Book).FirstOrDefaultAsync(b=>b.BookID==id);
Book book = await db.Books.FindAsync(id);
BookModel model = GetBooks.Details(book);
if (book == null)
{
return HttpNotFound();
}
return View(model);
}
以及我想显示数据的视图部分 Details.cshtml
<dd>
@Html.DisplayFor(model => model.Attribute_Book)
@{
var extAtt = Model.Attribute_Book.FirstOrDefault();
if (extAtt != null)
{
@extAtt.ValueTypeText
}
}
</dd>
详细信息视图正在使用我的 ViewModel,我认为它可能会有所帮助,但仍然没有帮助,我的功能是获取视图模型的详细信息
public static BookModel Details(Book item)
{
var model = new BookModel
{
BookID = item.BookID,
Title = item.Title,
PagesCount = Convert.ToInt32(item.PagesCount),
Description = item.Description,
Price = item.Price,
CountryID = item.CountryID,
AuthorID = item.AuthorID,
Author = item.Author,
Country = item.Country,
Picture = item.Picture,
TotalPrice = item.Country.TelCode + item.Price,
Attribute_Book = item.Attribute_Book.ToList()
};
return model;
}
这是它向我展示的内容
System.Data.Entity.DynamicProxies.Attribute_E4A8D634F0CCC98D73ED369A80817849BFA813311536941614A7242B3A2751A2 456
最后 3 个数字是我想要得到的值))
所有问题都在眼前。刚刚删除了这部分 @Html.DisplayFor(model => model.Attribute_Book)
并且它有效,我想 displayfor 正在获取主键 id 之后的第一个数据值,它是 AttributeID 并试图显示它。
我有 2 个 tables,书籍和属性,它们与 table Attribute_Book 之间存在多对多关系。我没有设法执行创建功能,只是想在详细信息视图中显示来自第 3 个 table 的数据。我手动放置它只是为了检查并遇到问题,除了我想显示的值之外,它还显示一个代理,它是属性名称(用断点看到它)。项目首先是数据库。感谢您的帮助。
这是我的 Books ViewModel
public BookModel()
{
this.Attribute_Book = new HashSet<Attribute_Book>();
}
public int BookID { get; set; }
public string Title { get; set; }
public int AuthorID { get; set; }
public int CountryID { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public Nullable<int> PagesCount { get; set; }
public string Picture { get; set; }
public decimal TotalPrice { get; set; }
public virtual ICollection<Attribute_Book> Attribute_Book { get; set; }
public virtual Author Author { get; set; }
public virtual Country Country { get; set; }
这是我的属性模型
public Attribute()
{
this.Attribute_Book = new HashSet<Attribute_Book>();
}
public int AttributeID { get; set; }
public string Name { get; set; }
public int AttTypeID { get; set; }
public virtual ICollection<Attribute_Book> Attribute_Book { get; set; }
public virtual Attribute_Type Attribute_Type { get; set; }
这是我的 Attribute_Book 模型
public int ID { get; set; }
public int BookID { get; set; }
public int AttributeID { get; set; }
public string ValueTypeText { get; set; }
public Nullable<System.DateTime> ValueTypeDate { get; set; }
public virtual Attribute Attribute { get; set; }
public virtual Book Book { get; set; }
这是我在控制器中的详细操作
public async Task<ActionResult> Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
//Book book = await db.Books.Include(b=>b.Attribute_Book).FirstOrDefaultAsync(b=>b.BookID==id);
Book book = await db.Books.FindAsync(id);
BookModel model = GetBooks.Details(book);
if (book == null)
{
return HttpNotFound();
}
return View(model);
}
以及我想显示数据的视图部分 Details.cshtml
<dd>
@Html.DisplayFor(model => model.Attribute_Book)
@{
var extAtt = Model.Attribute_Book.FirstOrDefault();
if (extAtt != null)
{
@extAtt.ValueTypeText
}
}
</dd>
详细信息视图正在使用我的 ViewModel,我认为它可能会有所帮助,但仍然没有帮助,我的功能是获取视图模型的详细信息
public static BookModel Details(Book item)
{
var model = new BookModel
{
BookID = item.BookID,
Title = item.Title,
PagesCount = Convert.ToInt32(item.PagesCount),
Description = item.Description,
Price = item.Price,
CountryID = item.CountryID,
AuthorID = item.AuthorID,
Author = item.Author,
Country = item.Country,
Picture = item.Picture,
TotalPrice = item.Country.TelCode + item.Price,
Attribute_Book = item.Attribute_Book.ToList()
};
return model;
}
这是它向我展示的内容
System.Data.Entity.DynamicProxies.Attribute_E4A8D634F0CCC98D73ED369A80817849BFA813311536941614A7242B3A2751A2 456
最后 3 个数字是我想要得到的值))
所有问题都在眼前。刚刚删除了这部分 @Html.DisplayFor(model => model.Attribute_Book)
并且它有效,我想 displayfor 正在获取主键 id 之后的第一个数据值,它是 AttributeID 并试图显示它。