Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect use of Model vs Controller in MVC / ASP.NET MVC
    primarykey
    data
    text
    <p>I have a Service class with a method called GetProducts(). That encapsulates business logic and calls the repository to get a list of products.</p> <p>My MVC view wants to show that list of products as an MVC SelectList. Where is the correct place for that logic to go. I seem to have 3 options:</p> <ol> <li><p><strong>Model</strong></p> <p>The Model should expose a property called <code>ProductSelectList</code>. When the getter of this property is called by the View, the Model should call <code>Service.GetProducts()</code> and convert the result to a SelectList before passing it on. </p> <p>Plausible argument: <em>The Model should make calls to business logic and the repository. The View should merely render predetermined data. The Controller should not be involved, save for passing contextual data to the Model.</em></p></li> <li><p><strong>View</strong></p> <p>The View should contain code that calls <code>Service.GetProducts()</code> directly and converts the result to a SelectList inline.</p> <p>Plausible argument: <em>The View should call for this data directly as it is specifically for use on the View. There is no need to involve the Model or Controller, as we are calling an abstracted Service method anyway, so anything else just adds extra overhead.</em></p></li> <li><p><strong>Controller</strong></p> <p>The Controller should make the call to <code>Service.GetProducts()</code>, convert the results to a SelectList and pass it through to the Model, which should contain a simple <code>ProductSelectList</code> property. The View will access this property for rendering.</p> <p>Plausible argument: <em>The Controller knows which parameters to provide to the Service method, so it should make the call. The Model should be a simple placeholder for data, filled by the Controller. The View's job is to simply render the data from the Model.</em></p></li> </ol> <p>I have a <em>feeling</em> that the correct answer is <strong>Model</strong>, but the other two make some reasonable points. Perhaps I've muddied the waters by already having a Service class that's separate to the Model?</p> <p>Would anybody care to share their opinion? Is this just a matter of taste?</p>
    singulars
    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.
 

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