Which book cover is better?

You're great, thank you!

Asp.net mvc3: How to pull data from database and fill view model in one goal?

Assuming your ViewModel looks like this: public class ViewModel { IEnumerable lst { get; set; } // other properties } Just do this: public ActionResult GetMonies() { var monies = context. Money . Take(10) .ToList(); var model = new ViewModel { lst = monies }; return View(model); }.

stackoverflow.com»

IEnumerable has the ToList() function which returns a list of said type. Vm. Lst = context.Money.

Take(10).ToList(); // returns a List.

stackoverflow.com»

I would recommend you to prevent possibility of changing ViewModel. Lst, i.e. Lst's type should be IEnumerable instead of List/IList (of course if your further code doesn't require List functionality).

Furthermore, I suppose you don't modify lst reference, so you could remove setter and initialize lst via constructor. Public class ViewModel { public ViewModel(IEnumerable lst) { this. _lst = lst; } private readonly IEnumerable _lst; IEnumerable Lst { get { return this.

_lst; } } // other properties } public ActionResult GetMonies() { var model = new ViewModel(context.Money. Take(10).ToArray()); return View(model); } This approach guarantees that consumers of your code will not modify your ViewModel. Lst accidentally.

stackoverflow.com»

I would recommend you to prevent possibility of changing ViewModel. Lst's type should be IEnumerable instead of List/IList (of course if your further code doesn't require List functionality). Furthermore, I suppose you don't modify lst reference, so you could remove setter and initialize lst via constructor.

This approach guarantees that consumers of your code will not modify your ViewModel.

stackoverflow.com»

Related Questions