Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC ViewModel with methods - is it "legal"?

Tags:

Should viewmodels be limited to only have properties, and not methods?

Let's say I have a radio button in my view, and wants to see if the radio button should be checked.

I could do this entirely in my view:

@Html.RadioButton("radiobuttonName", "The value", (id == Model.PersonId))  

or I could move this logic into the viewmodel:

@Html.RadioButton("radiobuttonName", "The value", Model.IsChecked(id) 

using this method in the viewmodel:

    public int PersonId { get;set;}     public bool IsChecked(int id)     {         return (id == PersonId);     } 

Is this OK to do, or should it be done entirely in the view, or in some other way?

like image 654
Buginator Avatar asked Jul 18 '11 14:07

Buginator


People also ask

Can a ViewModel have methods?

You can have methods in your ViewModel .

Can action method be private in MVC?

Action Method can not be a private or protected method. If you provide the private or protected access modifier to the action method, it will provide the error to the user, i.e., “resource can not be found” as below. An action method cannot contain ref and out parameters.

Can a action method be private?

All the public methods in the Controller class are called Action methods. The Action method has the following restrictions. - Action method must be public. It cannot be private or protected.

Is a ViewModel the same as a controller?

While the ViewModel is an optional pattern the Controller is a must, if you are going the MVC way. The ViewModel encapsulates presentation logic and state, The Controller orchestrates all the Application Flow.


2 Answers

You can have methods in your ViewModel. If it's a single result you want to calculate each time then I'd suggest adding the evaluation code to your Controller and storing the result in the ViewModel instead but if you need to evaluate things using a method more dynamically and a Property can't do this for you then doing this in the ViewModel is probably fine.

In your example above I'd recommend doing this in the ViewModel as then the ViewModel contains the logic in a single place rather than doing this many times copy and pasted in your View.

like image 74
Chris Snowden Avatar answered Nov 13 '22 05:11

Chris Snowden


That's a really good question regarding the correct placement of logic. Your approach is certainly legal - but does it follow the spirit of MVC? :)

I'd say it depends on whether the logic in your method applies just to this View/ViewModel, or could be potentially applied to other ViewModels that deal with this underlying Model type (in your case, Person).

If this is a one-off calculation for the purposes of this specific ViewModel, keep it in the Model. If this calculation might be used for Person objects generally, consider using a static Service class e.g. PersonService, and place your methods there.

like image 24
James McCormack Avatar answered Nov 13 '22 06:11

James McCormack