Click here to Skip to main content
11,717,883 members (82,399 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET MVC3
Hi everyone

I'm building an MVC3 application. I have my data models defined, and now I have to create ViewModels for specific actions.

I'm using automapper. Here's an example situation:

public class modelname{
  public int id {get;set;}
  public string name {get;set;}
  public string otherfield {get;set;}
  //many other properties
public class modelnameEditViewModel{
  public int id {get;set;}
  public string name {get;set;}

protected void Application_Start(){
   AutoMapper.Mapper.CreateMap<Models.Job, Models.JobEditViewModel>();
   AutoMapper.Mapper.CreateMap<Models.JobEditViewModel, Models.Job>();

//get the model from db, returns a strongly-typed partialview for viewmodel;

public ActionResult Edit(int id)
  modelname model = db.modelname.find(id);
  modelnameEditViewModel viewmodel = Automapper.Mapper.Map<modelname, modelnameEditViewModel>(model);
 return PartialView("PartialEdit", viewmodel);

Is it correct to do the following to update the model with only the properties of ViewModel?
What is the "best practice" for where to place my ViewModels? can I keep them in the same file along with the Model?

Thanks in advance,

Additional Tags are added.
Posted 12-Dec-11 7:49am
Edited 12-Dec-11 13:19pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

A ViewModel class is there to encapsulate multiple pieces of data represented by instances of classes into one easy to manage object that you can pass to your View.

There are no good place to keep your models in. You can keep them in separate assembly if the project is big and there are a lot of ViewModels (Data Transfer Objects). Also you can keep them in separate folder of the site project.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I don't know why, my post has been edited and the most important part of the question is missing..

The question about updating the main model via a ViewModel that has "some" of the properties of the main model.. is it correct to do it this way?

//http post from a strongly-typed partial view for ViewModel
public ActionResult Edit(modelnameViewModel viewmodel)
   //get the original model from db
   modelname model = db.modelname.find(id);
   Automapper.Mapper.Map<modelnameeditviewmodel,>(viewmodel, model);//"maps" the values of the properties of the posted viewmodel into the "main" model

 return PartialView("FullDetails", model);

This actually works, but I'm not shure about it being "formally correct" or not

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 764
1 OriginalGriff 215
2 F-ES Sitecore 210
3 Maciej Los 140
4 Jochen Arndt 120
0 Sergey Alexandrovich Kryukov 1,494
1 OriginalGriff 535
2 Maciej Los 529
3 Richard MacCutchan 440
4 CHill60 415

Advertise | Privacy | Mobile
Web01 | 2.8.150901.1 | Last Updated 13 Dec 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100