2013-04-15 82 views
0

我有兩個表General_Info和Quantity。 General_Info有一個主鍵QuoteID並與Quantity(FK = General_InfoQuotesID)具有一對多關係。在我的詳細信息視圖中,我想從General_Info中顯示來自Quantity的所有數據的特定行,其中來自General_Info的QuoteID等於General_InfoQuotesID。我目前在這方面的嘗試有一個Details視圖,它被設置爲顯示來自General_Info的指定行數據和一個保存數量的局部視圖。我遇到的問題是,不是顯示與General_Info中給定行相對應的數量,而是顯示所有添加的數量,而不管General_Info中對應的行是多少。當外鍵等於主鍵時在視圖中顯示數據

這裏是我的模型類:

public partial class General_Info 
{ 
    public General_Info() 
    { 
     this.Quantities = new HashSet<Quantity>(); 
    } 

    public int QuoteID {get; set;} 
    public System.DateTime Open_Quote {get; set;} 
    public string Customer_Name {get; set;} 
    ... 
    public virtual ICollection<Quantity> Quantities {get; set;} 
} 

public partial class Quantity 
{ 
    public int QuantityID {get; set;} 
    public int AdditionalQty {get; set;} 
    public int General_InfoQuotesID {get; set;} //fk 

    //navigation property 
    public virtual General_Info General_Info {get; set;} 
} 

然後這裏是我的一些控制器:

[HttpGet] 
public ActionResult Details(int id = 0) 
{ 

    General_Info general_info = unitOfWork.General_Info_Repository.GetByID(id); 
    return View(general_info); 
} 

[HttpGet] 
[ChildActionOnly] 
public ActionResult DQuantities() 
{ 
    IEnumerable<Quantity>quantities = unitOfWork.Quantity_Repository.Get(); 

    // from quantities, select additionalQty when 
    // General_Info.PK == Quantities.FK 
    var qtys = from q in quantities 
       where q.General_Info.QuoteID == q.General_InfoQuotesID 
       select q.AdditionalQty; 

我不認爲這條線是對的,但我試圖加載後試了一下linq聲明直接進入局部視圖不起作用

qtys = qtys.ToList(); 

    return PartialView(qtys); 
} 

然後在我的DQuantities視圖(我didn 「T包括細節來看,因爲這已經正常工作,並且我不認爲這是問題的所在反正)

@model IEnumerable<SourceMvc.Quantity> 

<table> 
    <tr> 
    <th> 
     Additional Quantities: 
    </th> 
    </tr> 

很肯定這foreach語句只是自IEnumerable目標 實際的表格數據 也就是爲什麼它的全部打印量爲並列 只是針對數據提前拾起我的LINQ語句

@foreach (var item in Model) { 
    <tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.AdditionalQty) 
    </td> 
    </tr> 
} 
</table> 

由於任何人誰可以提供幫助。

+0

你爲什麼不使用'公共虛擬的ICollection 數量{獲得;設置;}'屬性? –

回答

0

好吧,我有一個工作答案。

在我的控制,我改變了DQuantities到:

[HttpGet] 
    [ChildActionOnly] 
    public ActionResult DQuantities(int id = 0) 
    { 
     General_Info general_info = unitOfWork.General_Info_Repository.GetByID(id); 

     IEnumerable<Quantity> getQuantities = unitOfWork.Quantity_Repository.Get(); 

     IEnumerable<Quantity> qtys = getQuantities.Where(q => q.General_InfoQuotesID == general_info.QuoteID); 

     return PartialView(qtys); 
    } 

然後在我的部分觀點:

@model IEnumerable<SourceMvc.Quantity> 

<table> 
    <tr> 
    <th> 
     @Html.DisplayNameFor(model => model.AdditionalQty) 
    </th> 
    </tr> 

    @foreach (var item in Model) 
    { 
    <tr> 
     <td> 
     @Html.DisplayFor(modelItem => item.AdditionalQty) 
     </td> 
    </tr> 
    } 

</table>