What is the role of Model in MVC?

I read several articles about MVC, but there is one thing that I don’t understand. What is the role of the model in practical terms.

Is the model represented by a business object? Or is it just a class that helps send information from the controller to the view?

Take, for example, two business classes (data populated from a database)

Class Image
    Property FileName As String
    Property CreatedBy As User
End Class

Class User
    Property UserName as String
End Class

Will the “image” be a model or should I create a new class?

In the model, create a UserName property that will retrieve data from the User?

Class ImageModel
    Property FileName As String
    Property CreatedBy As User

    ReadOnly Property UserName As String
        Get
            Return User.UserName
        End Get
    End Property
End Class
+5
source share
2 answers

There are many opinions on this subject, but, in my experience, there are 2 main types Model:

ViewModel

POCO, , View. Controller.

, Skinny Controller

Model -. , View, Controller ..

MVC

MVC , ! , ... ViewState, Model, ViewModel, Model s, . . , - , , .

MVC, . , , Fat Model, Skinny Controller.
, " " " ", " ".
:

    • , HTTP- - , , , ..
    • "" Model Model . .
    • HTML JSON
    • Model
    • , " ", .
    • , View,

, MVC, , MVC , .

Model. , , .

public class UsersModel
{
    protected UserBusiness userBusiness = new UserBusiness();

    public UsersModel(string editUserName)
    {
        // Load all users:
        this.Users = userBusiness.GetAllUsers();

        // Load the user to be edited:
        this.EditUser = (editUserName == null) ? null : userBusiness.GetUser(editUserName);
    }

    public List<User> Users { get; private set;}
    public User EditUser { get; private set; }
}

" -" ( "-" ), . ... - .

+10

. - ( ) (Controller → Datasource ), - ( ) (Controller to View ).

ImageModel.

+1

All Articles