Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Not a good way to do it like you want to (in your original post). A view should always have a view model. A view model represents only the data that you want to have on the view, nothing more and nothing less. Do not pass your domail model to the view, but rather use a view model. This view model might contain just a portain of the properties of your domain model.</p> <p>In your list view you probably have a grid, and next to each row you probably have a details link, or a link on the name (as you have it). When either of these links are clicked then you are directed to a details view. This details view will have its own view model with only the properties that you need to display on the details view.</p> <p>A domail model might look something like:</p> <pre><code>public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } public string ExampleProperty1 { get; set; } public string ExampleProperty2 { get; set; } public string ExampleProperty3 { get; set; } } </code></pre> <p>Let say you only want to display the person's id, first name, last name and age then your view model will look like this:</p> <pre><code>public class PersonDetailsViewModel { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public int Age { get; set; } } </code></pre> <p>You don't need ExampleProperty1, ExampleProperty2 and ExampleProperty3 because they are not required.</p> <p>Your person controller might look like this:</p> <pre><code>public class PersonController : Controller { private readonly IPersonRepository personRepository; public PersonController(IPersonRepository personRepository) { // Check that personRepository is not null this.personRepository = personRepository; } public ActionResult Details(int id) { // Check that id is not 0 or less than 0 Person person = personRepository.GetById(id); // Now that you have your person, do a mapping from domain model to view model // I use AutoMapper for all my mappings PersonDetailsViewModel viewModel = Mapper.Map&lt;PersonDetailsViewModel&gt;(person); return View(viewModel); } } </code></pre> <p>I hope this clears things up a little more.</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