I need to build some Web screens with classic MVC architecture, containing Index page (list all instances of my class), Details (Check details of my class), Edit (Edit class data) and Delete (delete class instance).
I´m using Viewmodel and Automapper. I´m in a doubt about the correct way to assing ViewModels and Automapper in that scenario, as the index will have a collection of class and each other view will have just one instance:
Proposes Codes:
OPTION1: ONE CLASS FOR EACH VIEW:
public class Person
{
int id;
string Name;
int Age;
}
public class PersonViewModel
{
int id;
[Display(name = "Person name")]
string Name;
[Display(name = "Person age")]
int Age;
}
public class PersonIndexViewModel
{
List<PersonViewModel> Personlist;
}
PersonController:
{
var personList = db.List(); // Get data from db
PersonIndexViewModel indexview = new PersonIndexViewModel();
foreach (var item in personList)
{
var tempview = new PersonViewModel();
Map.Create (...); << Here ? How ?
indexview.PersonList.Add (tempview);
}
return view (indexview);
}
OPTION2: ONE VIEWMODEL:
public class Person
{
int id;
string Name;
int Age;
}
public class PersonViewModel
{
int id;
[Display(name = "Person name")]
string Name;
[Display(name = "Person age")]
int Age;
}
PersonController:
{
var personList = db.List(); // Get data from db
List<PersonViewModel> viewList = new List<PersonViewModel>();
foreach (var item in personList)
{
var tempview = new PersonViewModel();
Map.Create (...) // Here ? How ??
viewList.Add (tempView);
}
return view (viewList);
}
Is there something wrong if option 2? What would the best practices be on that case ? Thanks!