Building Web Applications Using Entity Framework And MVC 5: Part Four

Just to give you a quick recap, In Part 1, I’ve demonstrated how to create a simple database from scratch, talked about a brief overview of ASP.NET MVC in general, demonstrated how to build a simple data access using the Entity Framework 6, and created a simple implementation of a Signup page in MVC. Part 2 of the series talked about the step-by-step process of creating a basic login page and creating a simple role-based page authorization within the MVC application. Part 3 of the series talked about how to do Fetch, Edit, Update and Delete (FEUD) operations in the application using jQuery and jQuery AJAX.

If you haven't gone through my previous articles, please refer to the following links,

In this series, we will create a page to allow users to modify their profile data. Let’s get started.

Adding the GetUserProfile() Method

To begin with, open “UserManager” class and add the following method below,

  1. public UserProfileView GetUserProfile(int userID) {  
  2.             UserProfileView UPV = new UserProfileView();  
  3.             using (DemoDBEntities db = new DemoDBEntities()) {  
  4.                 var user = db.SYSUsers.Find(userID);  
  5.                 if (user != null) {  
  6.                     UPV.SYSUserID = user.SYSUserID;  
  7.                     UPV.LoginName = user.LoginName;  
  8.                     UPV.Password = user.PasswordEncryptedText;  
  9.   
  10.                     var SUP = db.SYSUserProfiles.Find(userID);  
  11.                     if (SUP != null) {  
  12.                         UPV.FirstName = SUP.FirstName;  
  13.                         UPV.LastName = SUP.LastName;  
  14.                         UPV.Gender = SUP.Gender;  
  15.                     }  
  16.   
  17.                     var SUR = db.SYSUserRoles.Find(userID);  
  18.                     if (SUR != null) {  
  19.                         UPV.LOOKUPRoleID = SUR.LOOKUPRoleID;  
  20.                         UPV.RoleName = SUR.LOOKUPRole.RoleName;  
  21.                         UPV.IsRoleActive = SUR.IsActive;  
  22.                     }  
  23.                 }  
  24.             }  
  25.             return UPV;  
  26.         }  
The method above gets the specific user information from the database by passing the SYSUserID as a parameter. You may have noticed that the method returns a UserProfileView type which holds some properties from different tables.

Adding the EditProfile() Action Method

Now, open “HomeController” class and add the following action methods,
  1. [Authorize]  
  2.         public ActionResult EditProfile()  
  3.         {  
  4.             string loginName = User.Identity.Name;  
  5.             UserManager UM = new UserManager();  
  6.             UserProfileView UPV = UM.GetUserProfile(UM.GetUserID(loginName));  
  7.             return View(UPV);  
  8.         }  
  9.   
  10.           
  11.         [HttpPost]  
  12.         [Authorize]  
  13.         public ActionResult EditProfile(UserProfileView profile)  
  14.         {  
  15.             if (ModelState.IsValid)  
  16.             {  
  17.                 UserManager UM = new UserManager();  
  18.                 UM.UpdateUserAccount(profile);  
  19.   
  20.                 ViewBag.Status = "Update Sucessful!";  
  21.             }  
  22.             return View(profile);  
  23.         }  
The code above, is composed of two action methods. The first EditProfile() method will be invoked, once the page is requested and loaded to the browser. What it does is to get the user profile data by calling the GetUserProfile() method and passing the SYSUserID as the parameter. The second is the overload method which will be invoked during POST request, that is when you hit the Button to save the data. What it does is that it first checks for validity of the fields (if they are valid and not empty), then calls the method UpdateUserAccount() and passes the UserProfileView model from the View to that method. If you still remember from my previous article series, the UpdateUserAccount() method is where it executes the actual saving of data to your database.

You may also have noticed that both action methods are decorated with the [Authorize] attribute to ensure that both methods will only be accessible by authenticated users.

Adding the View

The next step is to generate the View for the profile page. To do this, right click on the EditProfile() method and select “Add View”. In the Add View dialog, supply the needed fields as shown in the figure below,

Adding the View
Figure 1: Add View dialog

Take note of the Model class field value. It should be “UserProfileView”. Now, click Add to scaffold the UI for you.

Visual Studio will generate all the controls in the View, based on the fields you defined from your Model (UserProfileView). This means that it will also generate unnecessary fields  we don’t want to edit, such as, the LOOKUPRoleID and IsRoleActive. Besides that, we will also need to provide a drop-down list for displaying the Gender field. So, make sure to update the generated HTML markup. It would look like the following:
  1. @model MVC5RealWorld.Models.ViewModel.UserProfileView  
  2.   
  3. @{  
  4.     ViewBag.Title = "EditProfile";  
  5.     Layout = "~/Views/Shared/_Layout.cshtml";  
  6. }  
  7.   
  8. <h2>Edit Your Profile</h2>  
  9.   
  10. @using (Html.BeginForm())  
  11. {  
  12.     @Html.AntiForgeryToken()  
  13.       
  14.     <div class="form-horizontal">  
  15.         <hr />  
  16.         <span class="alert-success">@ViewBag.Status</span>  
  17.         @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  18.         @Html.HiddenFor(model => model.SYSUserID)  
  19.   
  20.         <div class="form-group">  
  21.             @Html.LabelFor(model => model.RoleName, htmlAttributes: new { @class = "control-label col-md-2" })  
  22.             <div class="col-md-10">  
  23.                 @Html.DisplayFor(model => model.RoleName)  
  24.                 @Html.ValidationMessageFor(model => model.RoleName, "", new { @class = "text-danger" })  
  25.             </div>  
  26.         </div>  
  27.   
  28.         <div class="form-group">  
  29.             @Html.LabelFor(model => model.LoginName, htmlAttributes: new { @class = "control-label col-md-2" })  
  30.             <div class="col-md-10">  
  31.                 @Html.EditorFor(model => model.LoginName, new { htmlAttributes = new { @class = "form-control" } })  
  32.                 @Html.ValidationMessageFor(model => model.LoginName, "", new { @class = "text-danger" })  
  33.             </div>  
  34.         </div>  
  35.   
  36.         <div class="form-group">  
  37.             @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })  
  38.             <div class="col-md-10">  
  39.                 @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })  
  40.                 @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })  
  41.             </div>  
  42.         </div>  
  43.   
  44.         <div class="form-group">  
  45.             @Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })  
  46.             <div class="col-md-10">  
  47.                 @Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })  
  48.                 @Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })  
  49.             </div>  
  50.         </div>  
  51.   
  52.         <div class="form-group">  
  53.             @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })  
  54.             <div class="col-md-10">  
  55.                 @Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })  
  56.                 @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })  
  57.             </div>  
  58.         </div>  
  59.   
  60.         <div class="form-group">  
  61.             @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })  
  62.             <div class="col-md-10">  
  63.                 @Html.DropDownListFor(model => model.Gender, new List<SelectListItem> {  
  64.                     new SelectListItem { Text="Male"Value="M" },  
  65.                     new SelectListItem { Text="Female"Value="F" }  
  66.                 }, new { @class = "form-control" })  
  67.             </div>  
  68.         </div>  
  69.   
  70.         <div class="form-group">  
  71.             <div class="col-md-offset-2 col-md-10">  
  72.                 <input type="submit" value="Save" class="btn btn-default" />  
  73.             </div>  
  74.         </div>  
  75.     </div>  
  76. }  
  77.   
  78. <div>  
  79.     @Html.ActionLink("Back", "Welcome")  
  80. </div>  
The markup above is a strongly-typed View which renders the UserProfileView model. Now, add the following markup in the “Welcome.cshtml”.
  1. @Html.ActionLink("Edit Profile", "EditProfile", "Home")  
The markup, above, is nothing but a link to the Edit Profile page, so that when you log in, you can easily navigate to your profile page and start modifying data.

Running the Application

Now, try to build your own code and run your application. The output should look similar to the figure below:

Edit Profile page
Figure 2: The Edit Profile page

After modifying the data

successful update
Figure 3: After successful update

It's that simple! I hope someone finds this post useful.

Up Next
    Ebook Download
    View all
    Learn
    View all