It is a good or bad idea to have a basic model in ASP.NET MVC

In almost every page on my ASP.NET MVC site, I use user info. This usually causes information about the company they are working with in the Model to go to the view or the view to determine which parts of the page users can view with their permissions.

Rather than getting the user in each model, is there a way to easily use the user's data all the time? That is, should you create a base model or have some kind of "master" controller that passes the user through the ViewData?

Any thoughts, or even better code, would be greatly appreciated.

+2


source to share


2 answers


This is a great idea. I use MasterModel consistently in all my MVC projects.

To do this, you need to subclass all of your viewmodels and also hardcode MasterPage for the MasterModel.



public class MasterModel
{
    string UserName { get; set; }
}

public class HomeModel : MasterModel
{
}

public class NewsModel : MasterModel
{
}

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MasterModel>" %>

User name: <%= Model.UserName %>

      

+4


source


This is a good idea if you are not reinventing the wheel. Quite a lot of information that people often put in sight (in particular, there is an ASP.NET user here, but people also seem to duplicate route data) is already available for view without adding it to the model. But for things that are not yet visible, this is fine.



0


source







All Articles