asp.net mvc - Should I have each ViewModel for each entity in my ASP MVC project? -
i have read many many solution can't understand or when use view model? example, when have register form user register, want hava field confirm password
, don't think should add user
entity. have viewmodel:
public class registerviewmodel { public user user { get; set; } public ienumerable<selectlistitem> city { get; set; } public ienumerable<selectlistitem> ward { get; set; } [required(errormessage = "bạn chưa nhập lại mật khẩu.")] [stringlength(100, errormessage = "mật khẩu phải có ít nhất {2} ký tự.", minimumlength = 6)] [datatype(datatype.password)] [system.web.mvc.compare("user.password", errormessage = "mật khẩu không khớp.")] public string confimpass { get; set; } }
so after read link how implement "confirm password" in asp.net mvc 3? . don't know why should replace password field in user
entity. i'm using unobstrusive client validation work if use model view. in view, must use m=> m.user.username
not m=>m.username
, etc... because of this, validation such compare password, or remote validation not work name in view m=>m.user.username
. wrong structure or model view in thinking?
there no single rule , need stay pragmatic, having said viewmodel , model (or domain model) 2 different things. no don't pollute entities placing properties don't belong them. idea ui should interchangeable , domain should not in way depend on it. dependencies should inverted. maybe tomorrow you'd switch (or extend) ui layer wpf (for example) ? current viewmodels (with attributes) wouldn't make sense. in case, yes should creating view model , keep relevant view in them, after map/pass values domain model. hope i'm making sense, let me know if need clarifications.
in case i'd create flattened registerviewmodel
include information needed register user, example:
public class registerviewmodel { [required] public string displayname { get; set; } [required] public string firstname { get; set; } // etc ... public ienumerable<selectlistitem> city { get; set; } public ienumerable<selectlistitem> ward { get; set; } [required(errormessage = "bạn chưa nhập lại mật khẩu.")] [stringlength(100, errormessage = "mật khẩu phải có ít nhất {2} ký tự.", minimumlength = 6)] [datatype(datatype.password)] [system.web.mvc.compare("user.password", errormessage = "mật khẩu không khớp.")] public string confimpass { get; set; } }
Comments
Post a Comment