2015-06-30 21 views
0

我想在一個表中顯示一組特定的N模型每個模型性能的有着多種模型對每個具體的屬性處理鑑於

我有以下型號:

public enum Rarity 
{ 
    Legendary, 
    Epic, 
    Rare, 
    Common 
} 

public class Card 
{ 
    public int ID { get; set; } 

    public string Name { get; set; } 
    public bool Disenchant { get; set; } 
    public Rarity Rarity { get; set; } 

    public virtual ICollection<Deck> Decks { get; set; } 
} 

甲板

public class Deck 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 

    public virtual Card Card { get; set; } 
    public virtual Hero Hero { get; set; } 
} 

首頁的索引控制器:

public ActionResult Index() 
{ 
    var db = new DustContext(); 
    var cards = db.Cards.ToList(); 
    return View(cards); 
} 

片段指數:

@model IEnumerable<App.Models.Card> 
<tbody> 
    @foreach (var item in Model) 
    { 
    @Html.Partial("_CardList", item) 
    } 
</tbody> 

,最後我的部分:

@model CardApp.Models.Card 

<tr> 
    <td>@Model.Name</td> 
    <td>@Model.Rarity</td> 
    <td>@Model.Disenchant</td> 
</tr> 

我怎樣才能在表格中顯示從卡模式(已在部分)的屬性一起有來自Deck模型的財產?我試過使用元組,但我得到列表和IEnumerable錯誤。

回答

1

您應該使用ViewModel並在ViewModel中使用每個模型的實例。

基本上,你的視圖模型將傳遞給您的視圖包含任意數量的其他實物,模型,性質,等等等等

http://sampathloku.blogspot.com/2012/10/how-to-use-viewmodel-with-aspnet-mvc.html

+0

可能一個ViewBag工作過什麼?對不起,我很新。 – Onilol

+1

是的,我想如果你想將你的模型放回到視圖包中,然後將視圖包放入視圖中,那就行了。但是,正確的方法和最佳實踐是使用視圖模型來處理服務器端代碼和視圖表單之間的交互。 – mituw16

+0

我的意思是......因爲我只想揭露Deck模型(Name)的一個特定屬性。 – Onilol

相關問題