ASP.NET MVC - Create a New Model or Use the Entity Infrastructure Class

I am developing an ASP.NET MVC 3 application, I first use the entity code to create the classes for my application, and I also have a repository to perform operations on it, while maintaining DBContext cleanup and DBEntities definitions.

I doubt the rendering of views and how to save the editing model.

If I have this object that represents a user stored in my DB:

//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; } } 

And I want to show a view with the names FirstName, LastName, Email and NewPassword, ConfirmPasword and CurrentPassword so that the user can change their data by typing CurrentPassword to confirm the changes, so I doubt that it contradicts ConfirmPasword and CurrentPassword aren't in my entities, so I need to create a new model for this view and copy the information I want from my new model to my database in order to save it? How:

 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; } } 

and in the controller I did:

 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(); } 

Is there a way to do this using a controller, for example:

 public ActionResult UpdateUser(User u) { repository.UpdateUser(u); return View(); } 

Because if I have this, how can I add a field like ConfirmPassword or CurrentPassword to do a check for this particular view.

+7
source share
2 answers

If I were you, I would not use the domain model in my presentation layer. I would create a view model (another class) that would be very similar to my domain model. Then I used the automatic matching tool to match from my domain model to the view model.

This is a very common scenario, so if you use Google for the View and Domain models, you should find everything you need.

 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; } 

Then you can configure auto-mapper to remove the boiler plate code:

 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); } 

This is very rude, but I hope you get an idea.

Update 1: **

As I understand it, it’s better to create a new model for each view, and then match it with an entity

It’s not just better, it provides a better separation of problems, makes your code easy to check. Just looking at the class name, I see its purpose (UpdateUserViewModel, RegisterUserViewModel, etc.).

The source fields in this class must be validated metadata, and isn't it?

By original fields, I mean:

 public class UserViewModel{ public string UserName { get; set; } public string FirstName { get; set; } } 

These fields are already in your User class, so I saved my time without typing them again.

Will it be changing my model from MVC to MVVM or not beacuse do I still have a controller?

I suppose that I still suggested the MVC pattern, not MVVM.

Regarding Automaper, are you using github.com/AutoMapper/AutoMapper?

Automapper is what I used. There are several tools, and they do almost the same thing. Try a little and find the one that best suits your needs.

Good luck.

+6
source

I usually use areas for different parts of my project, as far from where to put this additional code.

You will add quite a lot of the viewmodel.cs class to your model folder. Inside this class, your definitions of how the data will be modeled in the view will be indicated. These viewmodels will reflect the parts of the face with which you want the user to interact. Interaction will be performed in controllers through [HttpGet] , where you pass the view model that you want to interact with, and [HttpPost] , where you send the model back, and then match its entities.

ViewModels.cs:

 public class UserViewModel { public string UserName { get; set; } } 

SomeController:

 public ActionResult getView() { var uvm = new UserViewModel(); return View(uvm); } 

View getView.cshtml:

 @model project.namespace.UserViewModel @using (Html.BeginForm()) { @Html.EditorFor(m => m.UserName) <input type="submit" value="New User Name" /> } 

Back to the controller:

 [HttpPost] public ActionResult getView(UserViewModel model) { var entity = new ActualEntity(); entity.username = model.UserName; //more mapping //commit changes somewhere return RedirectToAction("getView"); } 
+1
source

All Articles