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

前端 未结 2 1135
时光取名叫无心
时光取名叫无心 2021-02-12 07:04

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 checke

2条回答
  •  太阳男子
    2021-02-12 07:17

    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.

提交回复
热议问题