如何分别获取查询的值

时间:2011-06-22 06:44:25

标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-3

在MVC3 asp.net中这是我的控制器声明: -

ViewBag.rawMaterialRequired = (from x in db.RawMaterial
join y in db.ProductFormulation on x.ID equals y.RawMaterialID
where y.ProductID == p select new { x.Description, y.Quantity });

这是与我相关的观察代码: -

@foreach(var album in ViewBag.rawMaterialRequired)
{
            @album<br />
}

所以输出是: -

{ Description = Polymer 26500, Quantity = 10 }
{ Description = Polymer LD-M50, Quantity = 10 } 
{ Description = Titanium R-104, Quantity = 20 }

但我需要这种答案: -

enter image description here

请建议我该怎么做? 提前谢谢你......

1 个答案:

答案 0 :(得分:1)

创建RawMaterial类

public class RawMaterial
{
    public string Description { get; set; }
    public int Quantity { get; set; }
}

并使用它代替匿名对象

ViewBag.rawMaterialRequired = (from x in db.RawMaterial
join y in db.ProductFormulation on x.ID equals y.RawMaterialID
where y.ProductID == p select new RawMaterial { x.Description, y.Quantity });

和视图

<table>
    <tr>
        <th>Description</th>
        <th>Quantity</th>
    </tr>

@foreach(var album in ViewBag.rawMaterialRequired)
{
    <tr>
        <td>@album.Description</td>
        <td>@album.Quantity</td>
    </tr>
}

</table>