c# - Returning three IQueryable lists in a single ActionResult to show in a view -
i'm new asp.net, mvc, c# , programming in general, have been doing personal projects , various tutorials learn.
i've hit problem not sure way turn solution or if indeed have gone down right road. code below best explains trying do. know viewmodel isn't right, think shows trying do. (alltransactions , vars various days have been snipped brevity).
var yesterdaystransactions = alltransactions.where(t => t.whenithappensid == yesterday); var todaystransactions = alltransactions.where(t => t.whenithappensid == today); var tomorrowstransactions = alltransactions.where(t => t.whenithappensid == tomorrow); var viewmodel = yesterdaystransactions.tolist() + todaystransactions.tolist() + tomorrowstransactions.tolist();
the intention show at-a-glance screen has list of yesterday's transactions @ top, today's in middle, , tomorrow's @ bottom.
i tried @html.partial @ first, of course showed me same list 3 times. tried if statements in view didn't feel right doing way repeating myself.
can offer advice areas should read , how should approach problem? in advance!
you're on right track - need view model. that's class used contain data need render view.
in case, do:
public class myactionviewmodel { public ienumerable<transaction> yesterdaystransactions { get; set; } public ienumerable<transaction> todaystransactions { get; set; } public ienumerable<transaction> tomorrowstransactions { get; set; } }
then in controller, populate instance of class:
var vm = new myactionviewmodel { yesterdaystransactions = alltransactions.where(t => t.whenithappensid == yesterday), todaystransactions = alltransactions.where(t => t.whenithappensid == today), tomorrowstransactions = alltransactions.where(t => t.whenithappensid == tomorrow), } ; return view( vm );
the last thing have tell view view model you're going give it:
@model myactionviewmodel
then can access data this
@model.todaystransactions
Comments
Post a Comment