ASP.net MVC - One ViewModel per View or per Action?

前端 未结 4 614
我寻月下人不归
我寻月下人不归 2020-12-31 07:27

Is it a better idea to have a single ViewModel per view or one per controller action?

Example:

public ProjectController : Controller
{
    public Act         


        
4条回答
  •  灰色年华
    2020-12-31 07:54

    The correct answer

    Neither. There's no silver bullet and shouldn't be.

    The correct answer is therefore: use as many view models as your user interface process demands. That's regardless of views or controller actions.

    Sometimes an action demands a view, other a view. But don't follow some strict guidelines that would hinder your development. View models will come naturally as you develop your application. And should. Otherwise you may end up with unreasonable views that are based on some guideline you've set in stone.

    This is actually a similar answer as @DarinDimitrov's, but with a direct conclusion.

提交回复
热议问题