Tuesday 22 October 2013

What is Model and ViewModel in MVC Pattern?

Model and ViewModel are two things we always hear in MVC. And in this post I am
going to show you the differences between them.

Let’s begin with its common definition.

What is Model or Domain Model?

Actually, the word 'model' has hundreds of meaning in software development, but
here we gone talk about 'model' in MVC design pattern. I would define model as an
object that we use to send information to the database, to perform business
calculations, to render view. In other word, 'model' represent the domain of
the application helps us in saving, creating, updating and deleting records. Usually
we put all our model classes in Model folder.

What is ViewModel?

ViewModel in MVC design pattern is very similar to 'model'. The major difference
between 'Model' and ‘ViewModel’ is that we use ViewModel only in rendering views.
We put all our ViewModel classes in ‘ViewModels’ named folder, we create this folder.

Understand it with example

Let's assume, we want to implement a view page that will have three textboxes for
Username, Password and Re-enter Password. To achieve this we could design a
'Model' as given below:

    public class Login
    {
        public String Username { getset; }
        public String Password { getset; }
        public String RePassword { getset; }
    }

For sake of view this model works fine. But this is actually a wrong approach because
we are trying to overcrowd the database. I can't see any use of 'RePassword'
property in database.

Now, if we take the advantage of ViewModel, we can safeguard the database from
overcrowding with fields. Here’s how, design following ‘Model’ which will be our
Domain Model:-

    //this will represent domain of the application
    public class Login
    {
        public String Username { getset; }
        public String Password { getset; }
    }

And then following 'ViewModel':-

    //this will help in rendering great views
    public class LoginViewModel
    {
        public String Username { getset; }
        public String Password { getset; }
        public String RePassword { getset; }
    }

Now, when adding view, pick the ViewModel class to get the strongly-typed benefits.


Now the question is how do we transform the Model or Domain Model from the
‘ViewModel’? Let’s learn it.

Transforming Model from ViewModel

There are various ways to do this. In the form POST action we can create a new
object of type Login model and then assign the properties one by one and leave the
unwanted properties.

[HttpPost]
public ActionResult Login(LoginViewModel viewModel)
{
    //validate the ViewModel

    //transforming Model (Domain Model) which is Login from ViewModel which is
        LoginViewModel
    var login = new Login()
    {
        Username = viewModel.Username,
        Password = viewModel.Password
    };

    //save the login var

    return View();
}

In above code, after validating the ViewModel I’m transforming the Model or Domain
Model. So, by using this way you can stop overcrowding database with
unwanted fields.

I Hope this will help you to understand the use of Model and ViewModel.




2 comments:

  1. Thanks for sharing this article.

    ReplyDelete
  2. đồng tâm
    game mu
    cho thuê nhà trọ
    cho thuê phòng trọ
    nhac san cuc manh
    số điện thoại tư vấn pháp luật miễn phí
    văn phòng luật
    tổng đài tư vấn pháp luật
    dịch vụ thành lập công ty trọn gói
    lý thuyết trò chơi trong kinh tế học
    đức phật và nàng audio
    hồ sơ mật dinh độc lập audio
    đừng hoang tưởng về biển lớn ebook
    chiến thắng trò chơi cuộc sống ebook
    bước nhảy lượng tử
    ngồi khóc trên cây audio
    truy tìm ký ức audio
    mặt dày tâm đen audio
    thế giới như tôi thấy ebook

    Trải qua vài lần giết người, hắn đã không còn do dự như trước nữa, thời buổi này là thời buổi của cường giả, ta không giết người, thì người cũng giết ta.

    Một kiếm vừa xuất, mười đạo kiếm quang như tia chớp nhằm phía nữ tử che mặt phóng tới.

    Nữ tử che mặt hối hận mình đã khinh địch, tin tức tình báo căn bản không có nói rõ về tu vi của Lưu Phong.

    “A——!”Nữ tử che mặt thân thể mềm mại tránh một kiếm của Lưu Phong, đột nhiên đôi chân lảo đảo, ngực nàng cảm thấy mát lạnh, không hay rồi, kiếm thế của Lưu Phong đã chém đứt một bên áo ngực của nàng.

    Đồ đàn ông hạ lưu.

    Vang lên tiếng áo rách, nhuyễn kiếm của Lưu Phong đã chém rách mấy lần áo của nữ tử che mặt, một bên ngực trắng như tuyết lộ ra.

    “Đồ vô sỉ——!”Nữ tử che mặt kinh giận mắng,che vội chỗ bị rách, căm giận nhìn Lưu Phong, nếu ánh mắt có thể giết người, Lưu Phong hắn sợ rằng đã chết một trăm lần rồi.

    Lưu Phong khóe miệng cười cười, sau đó ánh mắt chằm chằm nhìn vào ngực của nữ tử che mặt, nói: “ Tiếp tục chứ? Ta sẽ không ngại gì mà khoét một chỗ nữa trên ngực ngươi đâu.”

    “Vô sỉ。” Nữ tử che mặt lạnh lùng nói:“Ngươi cho rằng ta không giết được ngươi chăng?”

    ReplyDelete

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives