MVC2 Model design related question
I am working in asp.net MVC2 application and I have question related to design of model. I have an edmx file which has all the entities. I have a viewmodel named whiteoutviewmodel which uses Three list of entities and three other collections like this:
public class WhiteoutViewModel
{
public List<Entities.Server> Servers { get; set; }
public List<Entities.Whiteout> Whiteouts { get; set; }
开发者_开发百科 public List<Entities.Field> Fields { get; set; }
public ArrayList Hours {get;set;}
public ArrayList Minutes {get;set;}
public ArrayList AMPM {get;set;}
public ArrayList RepeatList{get;set;}
}
I have created a view based on this Viewmodel. Now I need a partial view which should be of type Whiteouts . I have created a whiteout class and added some properties in it.
public class WhiteoutDTO
{
public int WhiteoutId { get; set; }
public int FieldId { get; set; }
public int StartHour { get; set; }
public int StartMinute { get; set; }
public string StartTime { get; set; }
public int EndHour { get; set; }
public int EndMinute { get; set; }
public string EndTime { get; set; }
public string Repeats { get; set; }
public bool IsActive { get; set; }
public enmDays Days { get; set; }
}
Now I have two Whiteout class. One is from edmx and one that I created. Do I need to make my partial view of type edmx or the one I created ? The reason I created my class is that I needed some properties which are not in database table/edmx whiteout. Should I make my whiteout class as property of above ViewModel?
[EDIT]
I have a viewmodel which has List of whiteouts (edmx) and I also need single Whiteout so my viewmodel can have like this:
public class viewmodel {
// of edmx entity type
public List<Entities.Whiteout> Whiteouts { get; set; }
// of my DTO type where my DTO has some properties which are not in edmx whiteout entity
public WhiteoutDTO myWhiteout {get;set;}
}
also I want my main view of type Whiteouts where as my partial view of type myWhiteout . Is it possible and is good practice. Can I pass mywhitout from main view to partial view where main view is of Whiteouts type.
Right now I have partial view included like this:
Html.RenderPartial("WhiteoutList", Model);
Please suggest
I try and keep the view completely separate from anything related to Data Access and this includes anything generated by an ORM.
What you want is to keep your views as dumb as possible. ViewModels are there to only give your view what it needs and in return, give you some information back about some possibly entered data by the user.
Imagine for a second a Login Page, You have a User that's generated by Entity Framework and then you need some kind of mediator object that will transfer the data back and forth from your view. This being said, you don't want to create a strongly typed view to your generated User class from Entity Framework.
What you want is to create a LoginUserViewModel with just enough information for the view to be able to login the user and post the information back to the server for authentication and authorization.
The problem and ugly plumbing code that takes place is that you now have to manually map the properties from LoginUserViewModel to User.
What you can do is use something like AutoMapper which makes things a lot cleaner and easier.
i.e.
AutoMapper.Mapper.Map<AccountLoginViewModel, User>(accountLoginViewModel, user);
You can extend the model by using a partial class:
Public partial class Whiteout{
Public string NewStringProperty{get;set;}
}
精彩评论