如何在ASP.NET MVC剃刀中显示分组数据?

时间:2015-07-12 13:32:07

标签: c# asp.net-mvc linq razor

我的控制器中有这个代码:

var taxes = db.invoiceTaxes
    .Where(c => c.invoiceId == invoice.invoiceId)
    .GroupBy(row => new { row.TaxId, row.tax, row.percent, row.type })
    .Select(group => new { tax = group.Key.tax, percent = group.Key.percent, sum = group.Sum(s => s.sum), type = group.Key.type });

ViewBag.taxes = taxes.ToList();

如果我在我看来有这个

@foreach (var item in ViewBag.taxes)
{
    @item
}

我得到了

  

{tax = Facturacion.Models.Tax,percent = 16,sum = 31.36,type = False}

但是当我有

@foreach (var item in ViewBag.taxes)
{
    @item.percent
}

我得到了

  

对象不包含百分比

的定义

如何显示查询返回的字段? (在这种情况下,值“16”)

invoiceTaxes不是我的型号,它是发票

4 个答案:

答案 0 :(得分:1)

在控制器上,您可以创建匿名类型。问题是使用internal访问器创建的匿名类型的所有属性。所以你不能在View上使用它。

有关详细信息,请参阅this answer

答案 1 :(得分:1)

嗯,你可以(琐碎的样本,根据需要修改和改进):

NSMutableArray* images = [NSMutableArray new]; 
[images addObject:image1];
[images addObject:image2]; 
[images addObject:image3]; 
[images addObject:image4]; 
....

UIImage* second = [images objectAtIndex:1];

//Some dummy class to mock some data to play with
//You don't need this..I just need some data to play with for the sample
public class DummyData
{
    public string InstructorId { get; set; }
    public string Name { get; set; }
    public int Invoice { get; set; }
}

现在为Controller

public ActionResult DoDynamic()
{
    //Some data to use only for this sample (you already have some data to query, so you don't need this)
    var data = new List<DummyData>
    {
        new DummyData {Name = "Foo is my name", InstructorId = "Foo 1", Invoice = 1},
        new DummyData {Name = "Bar is my name", InstructorId = "Bar 1", Invoice = 2},
        new DummyData {Name = "Hello is my name", InstructorId = "Foo 2", Invoice = 1},
    };

    var query = data.Where(o => o.Invoice == 1).GroupBy(row => new { row.InstructorId }).Select(group => new { name = group.Key.InstructorId }).ToList();

    //Yeah, we can do dynamic :)
    dynamic foo = new ExpandoObject();
    foo.bar = "Hello from dynamic object";
    if (query.Any())
    {
        var result = "";
        query.ForEach(i => result += i.name + "<br />");
        foo.dyn = result;
    }

    //Lets use ViewBag
    ViewBag.SomeObject = foo;

    //And let's use model too...
    return View(foo);
}

结果:

Rendered view

希望这能帮到你......

答案 2 :(得分:0)

您可以尝试更改控制器代码,如下所示

var taxes = db.invoiceTaxes
    .Where(c => c.invoiceId == invoice.invoiceId)
    .GroupBy(row => new { row.TaxId, row.tax, row.percent, row.type })
    .Select(group => new { tax = group.Key.tax, percent = group.Key.percent, sum = group.Sum(s => s.sum), type = group.Key.type });

return View(taxes.ToList()); // Changed here from Viewbag to View(model)

在视图中

@model IEnumerable<Facturacion.Models.Tax>
//Some code here to define UI etc
@foreach (var item in Model)
{
    <td>
        @Html.DisplayFor(modelItem => item.percent)
    </td>

}

答案 3 :(得分:0)

您应该创建一个新的视图模型来保存值(如果您还没有)。

然后使用

 .Select(group => new InvoiceTaxViewModel { 
    tax = group.Key.tax, 
    percent = group.Key.percent, 
    sum = group.Sum(s => s.sum), 
    type = group.Key.type });

然后在您的视图中,您应该将ViewBag对象强制转换回List<Of Model>

@foreach (var item in (List<InvoiceTaxViewModel>)ViewBag.taxes)
{
    @item.percent
}

我可能会在此

之前的视图中创建一个新对象
@{
    var invoiceTaxList = ViewBag.taxes as List<InvoiceTaxViewModel>;
}

@foreach (var item in invoiceTaxList)
{
    @item.percent
}