1

I am working on ASP.net MVC . Here, M Stands for just "Model" . A Model that can be mapped to database.

Usually, i Strongly bind my View with Model .

But, recently i was hearing about View Model that is some thing different than just Model.

Is ViewModel Part of MVC or MVVM ( ModelViewViewModel) ?

What is the basic difference between ViewModel and Model. I would like to give a small example as how i work in MVC:

My View:

<% using (Ajax.BeginForm("TestAjax", "Reviewer", new AjaxOptions { OnSuccess = "Callback", OnFailure = "Failed" }))
   { %>
<table align="center">
    <tr>
        <td class="tdCol1Align">
            <label>
                Number1</label>
        </td>
        <td class="tdCol2Align">
            <%=Html.TextBoxFor(Model => Model.number1)%>
        </td>
    </tr>
    <tr>
        <td class="tdCol1Align">
            <label>
                Number2</label>
        </td>
        <td class="tdCol2Align">
            <%=Html.TextBoxFor(Model => Model.number2)%>
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="Add" class="button" />
        </td>
    </tr>
</table>
<%
    }
%>

My Model:

public class AddModel
    {
        public int number1
        {
            get;
            set;
        }

        public int number2
        {
            get;
            set;
        }

    }

and finally My Controller:

   [HttpPost]
        public JsonResult TestAjax(AddModel model)
        {

            int iSum = model.number1 + model.number2;
            return Json(new { Sum = iSum });

        }

That's it . I am not able to understand where this ViewModel comes into Picture

please clarify me on following:

1.The basic difference between ViewModel and Model

2.Is View Model Part of MVC or MVVM Architecture?

  1. How to implement above example incase of using ViewModels?

  2. If View Models are Part of MVC, then where will they be in the folder structure of the Application?

Sai Avinash
  • 4,683
  • 17
  • 58
  • 96
  • 1
    possible duplicate of [ASP.NET MVC Model vs ViewModel](http://stackoverflow.com/questions/4061440/asp-net-mvc-model-vs-viewmodel) – Renatas M. Oct 25 '13 at 11:31

4 Answers4

4

Is ViewModel Part of MVC or MVVM ( ModelViewViewModel) ?

ViewModel is the M in MVC.

What is the basic difference between ViewModel and Model.

A Model that can be mapped to database is not the M in MVC. This is a domain or business model. What you should pass to your views from a controller action is a view model. A single view model could be a combination of multiple domain models.

I am not able to understand where this ViewModel comes into Picture

In this example the view model is the anonymous type you created here:

return Json(new { Sum = iSum });

Basically it is a class with a single property called Sum. Maybe it would have been more clear if you defined an actual view model instead of using an anonymous type:

public class ResultViewModel
{
    public int Sum { get; set; }
}

that your controller action might have passed to the view:

[HttpPost]
public JsonResult TestAjax(AddModel model)
{
    int iSum = model.number1 + model.number2;
    ResultViewModel viewModel = new ResultViewModel();
    viewModel.Sum = iSum;
    return Json(viewModel);
}

but since in this case there's no actual view but a JSON result, using an anonymous type is just fine.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • @Darin.thanks..Great explanation. i got clarified now..but one question .Where do we place these view models in Application directory Structure . I mean like models,views,controllers etc.. – Sai Avinash Oct 25 '13 at 11:47
  • In the Controllers and Views folder it is obvious what should be placed. In the Models folder you place your ViewModels. The other is part of your application implementation and unrelated to the MVC pattern and default project structure. – Darin Dimitrov Oct 25 '13 at 12:15
  • @Darin.Can you Please help me on this:http://stackoverflow.com/questions/19592707/unable-to-update-label-hiddenfor-content-after-the-ajax-request?noredirect=1#comment29081574_19592707 – Sai Avinash Oct 25 '13 at 16:10
1

MVC's view model is not the same as MVVM's view model.

For MVC, a view model is a kind of model which is specific for one or several views. What we call usually "Model" are entities, also used by your business layer and your data access layer. But many times, they're not well suited for displaying data to users, because views may have specific needs. You may need to combine several entities, make some transformations, then you make a view model that only contains needed data. A view should get a view model with data ready to be displayed, it shouldn't have to do additionnal process.

Réda Mattar
  • 4,361
  • 1
  • 18
  • 19
1

The Model in MVC is not just a class (POCO) which represents one (or more) table in the database. It's a layer. It contains all the business, validation and data-access logic for your application. It also consists of domain models, these do represent database tables.

When your application grows and you need more flexibility and complex views, a domain model usually doesn't fit for using it in a view. Especially when you add validation attributes which are different from your database validation, or when your view contains properties of more than one domain model. Take a login view for instance, you don't want to use the User domain model here, but a specialized view model which contains just the properties you need for the view:

public class LoginModel
{
    [Required]
    public string Username { get; set; }    

    [Required]
    [DataType(DataType.Password)]
    public string Password { get; set; }

    public bool RememberMe  { get; set; }
}
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
1

The simple explanation is

Model : It's defined for you can define property for read values from Database ( Just like a data Access layer)

View Model : It's defined for you can define properties for reade write values to View(UI ) (just like a Business Logic Layer)

Edit:

If you use MVVM pattern , Then you will manually create a folder with name as ViewModel in solution explorer , But if you used MVC pattern ,Then the Model is automatically created in the solution explorer ,

The Model folder have database related classes(For include some methods for read and write data to database) , View Model have our business logical and UI related classes(for include some properties for read and write values with view(UI) ,and View have our html or any page In MVVM Pattern

The Model have Database classes and with business logical and UI related classes , and View have our html or any page in MVC Pattern

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234