我有2個表,Books和Attributes,它們通過與表Attribute_Book的多對多關係相互連接。我沒有設法執行創建功能,只想在詳細視圖中顯示來自第三個表的數據。我只是爲了檢查和解決問題,除了我想要顯示的值之外,還顯示了一個代理,它是屬性名稱(看到它帶有斷點).Project是db優先。謝謝你的幫助。Asp.net MVC,顯示來自db的數據(多對多)
這是我的書的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);
}
而且在這裏我想顯示的數據
<dd>
@Html.DisplayFor(model => model.Attribute_Book)
@{
var extAtt = Model.Attribute_Book.FirstOrDefault();
if (extAtt != null)
{
@extAtt.ValueTypeText
}
}
</dd>
詳細信息查看在使用我的視圖模型,我想也許這會有所幫助,但仍不能認爲Details.cshtml的一部分,我的功能從哪裏獲得詳細信息對於視圖模型
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號是我想))
如果你沒有指定關係的任何配置,實體框架爲你的關係生成代理 – Zinov
對不起,但我想知道,如果我有所有的外鍵,所有這些只是scafolding的結果,我必須添加什麼和項目在哪裏正常工作? – Hovo
https://msdn.microsoft.com/en-us/data/jj592886.aspx也看到這個參考http://stackoverflow.com/questions/7111109/should-i-enable-or-disable-dynamic-proxies -with實體框架-4-1-和MVC3。如果這幫助你讓我知道把它作爲一個答案 – Zinov