私は2つのテーブル、BooksとAttributesを持っていますが、それらはテーブルAttribute_Bookとの多対多の関係で互いに接続されています。私は、3番目のテーブルからのデータを詳細ビューで表示したいだけで、作成機能を管理しませんでした。私は手動でチェックして問題を得ました。値の横に属性名であるプロキシも表示しています(ブレークポイントで見ました).Projectはdbが最初です。助けてくれてありがとう。データベースからのデータを表示するAsp.net MVC
これはこれは、モデル
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; }
私の属性であり、これはこれは、コントローラ
の私の詳細アクションです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; }
私Attribute_Bookモデルである私の本の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 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>
は、私は多分考えて、私のViewModelを使用している、それは役立ちますが、それでもないデータ
を表示するビューDetails.cshtmlの一部と私は詳細を取得私の機能viewmodelそれは私System.Data.Entity.DynamicProxies.Attribute_E4A8D634F0CCC98D73ED369A80817849BFA813311536941614A7242B3A2751A2 456を示す何
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;
}
このため
最後の3つの数字私が取得したい値である))
リレーションシップのコンフィグレーションを指定しない場合、エンティティフレームワークは関係のプロキシを生成します – Zinov
申し訳ありませんが、私がすべての外部キーを持っていて、これがすべてスカッフォールディングの結果である場合は、プロジェクトが正しく働くためには何がどこですか? – Hovo
https://msdn.microsoft.com/en-us/data/jj592886.aspxこの参照も参照http://stackoverflow.com/questions/7111109/should-i-enable-or-disable-dynamic-proxies -with-entity-framework-4-1およびmvc3。それはあなたが私に答えとしてそれを置くことを知らせるのに役立つ場合 – Zinov