Note that there are some explanatory texts on larger screens.

plurals
  1. PO.NET MVC3 Preferred Model Initializiation
    text
    copied!<p>I have been working in MVC recently, and I am curious about what the best way to initialize my view models is. Should I be mapping it directly in the controller, or should I be initializing the properties in a constructor for the view model. Also, when having lists, is this better practice since you will not have to repopulate them when there is a validation error.</p> <p>For example, if i had the following model:</p> <pre><code>public FooBarViewModel { public int FooBarId { get; set; } public string SomeInitialProperty1 { get; set; } public string SomeInitialProperty2 { get; set; } public string SomeInitialProperty3 { get; set; } public string SomeInitialProperty4 { get; set; } public int FooId { get; set; } public int BarId { get; set; } public IEnumerable&lt;Foo&gt; Foos { get; set; } public IEnumerable&lt;Bar&gt; Bars { get; set; } } </code></pre> <p>and then the controller:</p> <pre><code>public MyController : Controller { [HttpGet] public ActionResult FooBar(int foobarId) { var foobar = _fooBarRepository.GetById(foobarId); var model = new FooBarViewModel { FooBarId = foobar.Id; SomeInitialProperty1 = foobar.SomeInitialProperty1; SomeInitialProperty2 = foobar.SomeInitialProperty2; SomeInitialProperty3 = foobar.SomeInitialProperty3; SomeInitialProperty4 = foobar.SomeInitialProperty4; Foos = foobar.Foos.ToList(); Bars = foobar.Bars.ToList(); } return View(model); } [HttpPost] public ActionResult FooBar(FooBarViewModel model) { if (ModelState.IsValid) { //process model return RedirectToAction("Index"); } var foobar = _fooBarRepository.GetById(model.FoobarId); model.Foos = foobar.GetFoos.ToList(); model.Bars = foobar.GetBars.ToList(); return View(model); } } </code></pre> <p>or should I do it in my model:</p> <pre><code>public FooBarViewModel { public int FooBarId { get; set; } public string SomeInitialProperty1 { get; set; } public string SomeInitialProperty2 { get; set; } public string SomeInitialProperty3 { get; set; } public string SomeInitialProperty4 { get; set; } public int FooId { get; set; } public int BarId { get; set; } public IEnumerable&lt;Foo&gt; Foos { get { return _foos; } } private IEnumerable&lt;Foo&gt; _foos; public IEnumerable&lt;Bar&gt; Bars { get { return _bars; } } private IEnumerable&lt;Bar&gt; _bars; public MyViewModel(FooBar foobar) { FooBarId = foobar.Id; SomeInitialProperty1 = foobar.SomeInitialProperty1; SomeInitialProperty2 = foobar.SomeInitialProperty2; SomeInitialProperty3 = foobar.SomeInitialProperty3; SomeInitialProperty4 = foobar.SomeInitialProperty4; _foos = foobar.Foos.ToList(); _bars = foobar.Bars.ToList(); } } </code></pre> <p>then my controller:</p> <pre><code>public MyController : Controller { [HttpGet] public ActionResult FooBar(int foobarId) { var foobar = _fooBarRepository.GetById(foobarId); var model = new FooBarViewModel(foobar); return View(model); } [HttpPost] public ActionResult FooBar(FooBarViewModelmodel) { if (ModelState.IsValid) { //process model return RedirectToAction("Index"); } return View(model); } } </code></pre> <p>Which is the preferred convention in MVC and why is it the best practice? Also, reasons why to choose one over the other? Thanks in advance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload