开发者

Binding pivot query to view in ASP.Net MVC

开发者 https://www.devze.com 2023-01-08 13:21 出处:网络
I\'m sticking on how to best present some data that\'s being dynamically generated from two different tables.

I'm sticking on how to best present some data that's being dynamically generated from two different tables.

Given my query:

    var assets = assetRepo.Find(x => x.LoginId == User.Identity.Name);
    var accounts = repository.Find(x => x.AccStatus == "A" && x.LoginId == User.I开发者_运维技巧dentity.Name);

    var query = from asst in assets
                join acct in accounts on asst.AccountId equals acct.AccountId
                select new
                {
                    Account = acct.AccountNumber,
                    Status = acct.AccStatus,
                    Make = asst.Make,
                    Model = asst.Model,
                    Submodel = asst.SubModel,
                    Registration = asst.Registration,
                    Balance = acct.BalanceOutstanding,
                    NextPayment = acct.NextPayment,
                    Date = String.Format("{0:dd MMM yyyy}", acct.NextPaymentDate),
                    Due = acct.ArrearsBal
                };

What would be the best (i.e. cleanest) way to bind this to the view? Would a custom class be required or is there a way to specify and iterate over a collection of anonymous types?


Creating custom class can give you additional benefits. You can use DisplayAttribute to set column headers and order. Then you can create view (or template to use with DisplayFor) that takes list of objects of any type and uses reflection to read annotations and display view nicely.

class Report {
    [Display(Name="Account",Order=1)]
    public string Account {get; set;}

    [Display(Name="Next payment",Order=2)]
    public Date NextPayment {get; set;}
}

It looks also clean. You will be able to use this annotations not only for grid, but also for excel exports or other data operations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号