我的疑问是关于视图的渲染以及保存编辑模型的方式.
//Entity: public class User { [Key] public int IdUser { get; set; } public string UserName { get; set; } public string Password { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } }
我想显示一个带有FirstName,LastName,Email和NewPassword,ConfirmPasword和CurrentPassword的视图,以便让用户更改他的数据,输入CurrentPassword来确认更改,所以我怀疑是,像ConfirmPasword和CurrentPassword这样的人在我的实体中,所以我需要为此View创建一个新模型,并将我想要的信息从我的新模型复制到我的数据库实体以保存它?喜欢:
public class UpdateUserModel { [required] [Display(Name = "Name")] public string FirstName{ get; set; } [required] [Display(Name = "Last Name")] public string LastName{ get; set; } [required] [DataType(DataType.EmailAddress)] [Display(Name = "Not valid email")] public string Email { get; set; } [DataType(DataType.Password)] [Display(Name = "New password")] public string NewPasword{ get; set; } [DataType(DataType.Password)] [Display(Name = "Confirm the New Pasword")] [Compare("NewPasword",ErrorMessage = "Password doesn´t mach.")] public string ConfirmPasword{ get; set; } [required(ErrorMessage = "Need to specify the current password to save changes")] [DataType(DataType.Password)] [Display(Name = "Current Password")] public string CurrentPassword { get; set; } }
在我做的控制器中:
public ActionResult UpdateUser(UpdateUserModel model) { User u = (User)Membership.GetUser(); u.FirstName = model.FirstName; u.LastName = model.LastName; u.Email = model.Email; if (!String.IsNullOrEmpty(model.NewPassword)) { u.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(model.NewPassword.Trim(),"md5"); } repository.UpdateUser(u); return View(); }
有任何方法可以做这样的控制器,如:
public ActionResult UpdateUser(User u) { repository.UpdateUser(u); return View(); }
因为如果我有,我如何添加像ConfirmPassword或CurrentPassword这样的字段,以便对此特定视图进行验证.
解决方法
这是一种非常常见的情况,因此如果您使用谷歌“视图和域”模型,您应该找到所需的一切.
public class User { [Key] public int IdUser { get; set; } public string UserName { get; set; } public string Password { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } } public class UpdateUserviewmodel { // Original fields public string Password { get; set; } public string PasswordConfirmation { get; set; }
public ActionResult ShowUser() { var domainModel = new User(); // I'm assuming that properties are set somewhere var viewmodel = new Userviewmodel(); Autommaper.Map(domainModel,viewmodel); return View(viewmodel); }
这非常粗糙,但希望你能有所了解.
更新1:**
As i understood is better to create a new model for each view and then map it into the entity
它不仅更好,它提供了更好的关注点分离,使您的代码易于测试.只需查看类的名称,我就可以看到它的用途UpdateUserviewmodel,RegisterUserviewmodel等).
Original fields,in this class is supposed to be the Metadata with the validation and that stuff isn’t?
原始字段我的意思是:
public class Userviewmodel{ public string UserName { get; set; } public string FirstName { get; set; } }
这些字段已经在您的User类中,所以我通过不再输入它来节省我的时间.
This will be change my model from MVC to MVVM or not beacuse i still have a controller?
我相信我所建议的仍然是MVC模式,而不是MVVM.
About the Automaper,are you using github.com/AutoMapper/AutoMapper?
Automapper是我用过的东西.那里有很少的工具,它们几乎完全相同.尝试一下,找到最适合您需求的产品.
祝好运.