Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I like to just use the default constructor on models for the view and populate them via <a href="https://github.com/AutoMapper/AutoMapper" rel="nofollow">Automapper</a>. </p> <p>I would have a view model like this:</p> <pre><code>public interface IHasDescription { public string DescriptionText { get; set; } } public class ViewModelType : IHasDescription { [DisplayName("This will be rendered in the view")] public string SomeText { get; set; } public string DescriptionText { get; set; } } </code></pre> <p>And I have a model from the DAL like this:</p> <pre><code>public class DALModelType { public string SomeText { get; set; } } </code></pre> <p>So you have something like this in your controller:</p> <pre><code>var dalModel = someRepository.GetAll(); var viewModel = Mapper.Map&lt;DALModelType, ViewModelType&gt;(dalModel); </code></pre> <p>And you have the Automapper setup code in some file. This way you only have the conversion code in one place instead of in multiple methods/controllers. You have a <a href="https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers" rel="nofollow">custom resolver</a> which uses dependency injection (instead of () => new CustomResolver()) and this will house your logic for getting the display text.</p> <pre><code>Mapper.CreateMap&lt;IHasDescription, ViewModelType&gt;() .ForMember(dest =&gt; dest.DescriptionText, opt =&gt; opt.ResolveUsing&lt;CustomResolver&gt;().ConstructedBy(() =&gt; new CustomResolver())); </code></pre> <p>Not sure if this works with your workflow but it <em>should</em> be able to get you what you want.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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