Note that there are some explanatory texts on larger screens.

plurals
  1. POASP.NET MVC display class content with a dropdown
    text
    copied!<p>In my NHIbernate (Database Model) I have this :</p> <pre><code>public class Pers { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} public string City{ get; set ;} public int Age{ get; set ;} public Role Role{ get; set ;} } </code></pre> <p>I have some dropwon (Database mode) :</p> <pre><code>public class Role { public int Id{ get; set ;} public string NL{ get; set ;} public string FR{ get; set ;} } </code></pre> <p>In my view I'd like use the dropdown and display some record (not all, in my real class there are much more properties) of <code>Pers</code>. I created a Dto class for Pers with the fields I need :</p> <pre><code>public class PersDto { public int Id{ get; set ;} public string FirstName{ get; set ;} public string LastName{ get; set ;} public RoleDto RoleDto{ get; set ;} } public class RoleDto { public int Id{ get; set ;} public string NL{ get; set ;} public string FR{ get; set ;} } </code></pre> <p><strong>In the controller :</strong></p> <pre><code>Mapper.CreateMap&lt;Role, RoleDto&gt;(); myModel.RoleDto = Mapper.Map&lt;Role, RoleDto&gt;(roleListFromDB); Mapper.CreateMap&lt;Pers, PersDto&gt;(); myModel.PersDto = Mapper.Map&lt;Pers, PersDto&gt;(persFromDB); public class MyModel{ public PersDto PersDto{ get; set ;} public RoleDto RoleDto{ get; set ;} } </code></pre> <p>Is it the right way ? Or it's better to do this with creating a <code>PersDto</code>: </p> <pre><code>public class MyModel{ public string FirstName{ get; set ;} public string LastName{ get; set ;} public RoleDto RoleDto{ get; set ;} } </code></pre> <p>Is it possible with automapper to copy only some fields and not all ?</p> <p>Thanks,</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