Let MVC Concatenate For You

We have all encountered the situation where we want to show user information on a form. Most people have a FirstName and a LastName (unless you're Prince or Bono) so we would usually show that data in two separate fields or columns.
 
Here is a way to quickly and easily concatenate MVC ViewModel fields without having to use String.Format. Let MVC concatenate information you.
 
Firstly, let me set the scene with a simple "ViewModelUser" ViewModel.
  1. namespace MyApp.Web.Models  
  2. {  
  3.     public partial class ViewModelUser  
  4.     {  
  5.         public int Id { getset; }  
  6.   
  7.         [Required("This is a required field")]  
  8.         [StringLength(50, ErrorMessageResourceName="ValidatedMaxLength", ErrorMessageResourceType=typeof(Generic))]  
  9.         [Display("UserName")]  
  10.         public string UserName { getset; }  
  11.   
  12.         [Required("This is a required field")]  
  13.         [StringLength(50, ErrorMessageResourceName="ValidatedMaxLength", ErrorMessageResourceType=typeof(Generic))]  
  14.         [Display("First Name")]  
  15.         public string FirstName { getset; }  
  16.   
  17.         [Required("This is a required field")]  
  18.         [StringLength(50, ErrorMessageResourceName="ValidatedMaxLength", ErrorMessageResourceType=typeof(Generic))]  
  19.         [Display("Surname")]  
  20.         public string LastName { getset; }  
  21.   
  22.         [Required("Email is a required field")]  
  23.         [StringLength(100, ErrorMessageResourceName="ValidatedMaxLength", ErrorMessageResourceType=typeof(Generic))]  
  24.         [Display("Email")]  
  25.         [RegularExpressEmailAttribute]  
  26.         [DataType(DataType.EmailAddress)]  
  27.         public string Email { getset; }  
  28.     }  
  29. }  
So now we have a number of fields that "describe" the ViewModelUser class.
 
A common use case for this is when we want to implement a simple CRUD mechanism. We want to make all users in the system available to an administrator and the most common way to do that is show a list screen, which displays all the users in a tabular format. The administrator can click one of the users in the table and will be directed to an Edit screen. Alternatively, the administrator could click a button to delete the intended user.

List Table

Let's take this table as an example.
 
 UseName First Name Last Name Email 
 Daffy Daffy Duck [email protected]Edit    Delete 
 Mickey Mickey Mouse[email protected] Edit    Delete 
 
You can see from this table that we have separated the first and last names. This is because we have separate fields in the database, which have flowed through to the ViewModel.
 
Here is the MVC razor code that renders the table. Take note of the fields in the razor code and how they relate to the fields of the ViewModelUser ViewModel.
  1. @model IEnumerable  
  2. <ViewModelUser>  
  3.     <table class="table table-bordered">  
  4.         <thead>  
  5.             <tr>  
  6.                 <th>    
  7.       @Html.DisplayNameFor(model => model.UserName)    
  8.    </th>  
  9.                 <th>    
  10.       @Html.DisplayNameFor(model => model.FirstName)    
  11.    </th>  
  12.                 <th>    
  13.       @Html.DisplayNameFor(model => model.LastName)    
  14.    </th>  
  15.                 <th>    
  16.       @Html.DisplayNameFor(model => model.Email)    
  17.    </th>  
  18.                 <th></th>  
  19.             </tr>  
  20.         </thead>  
  21.         <tbody>    
  22. @foreach (var item in Model)    
  23. {    
  24.   
  25.             <tr>  
  26.                 <td>    
  27.       @Html.DisplayFor(modelItem => item.UserName)    
  28.    </td>  
  29.                 <td>    
  30.       @Html.DisplayFor(modelItem => item.FirstName)    
  31.    </td>  
  32.                 <td>    
  33.       @Html.DisplayFor(modelItem => item.LastName)    
  34.    </td>  
  35.                 <td>    
  36.       @Html.DisplayFor(modelItem => item.Email)    
  37.    </td>  
  38.                 <td>  
  39.                     <input type="button" value="Edit" />  
  40.                     <input type="button" value="Delete" />  
  41.                 </td>  
  42.             </tr>    
  43. }    
  44.   
  45.         </tbody>  
  46.     </table>    
Wouldn't it be better use of screen real estate if we combined the first and last name columns into one? That is quite to do.
Firstly, let's make a change to the ViewModel.
  1. public int Id  
  2. {  
  3.     get;  
  4.     set;  
  5. }  
  6.   
  7. [Required("This is a required field")]  
  8. [StringLength(50, ErrorMessageResourceName = "ValidatedMaxLength", ErrorMessageResourceType = typeof(Generic))]  
  9. [Display("UserName")]  
  10. public string UserName   
  11. {  
  12.     get;  
  13.     set;  
  14. }  
  15.   
  16. [Required("This is a required field")]  
  17. [StringLength(50, ErrorMessageResourceName = "ValidatedMaxLength", ErrorMessageResourceType = typeof(Generic))]  
  18. [Display("First Name")]  
  19. public string FirstName  
  20. {  
  21.     get;  
  22.     set;  
  23. }  
  24.   
  25. [Required("This is a required field")]  
  26. [StringLength(50, ErrorMessageResourceName = "ValidatedMaxLength", ErrorMessageResourceType = typeof(Generic))]  
  27. [Display("Surname")]  
  28. public string LastName  
  29. {  
  30.     get;  
  31.     set;  
  32. }  
  33.   
  34. [Required("Email is a required field")]  
  35. [StringLength(100, ErrorMessageResourceName = "ValidatedMaxLength", ErrorMessageResourceType = typeof(Generic))]  
  36. [Display("Email")]  
  37. [RegularExpressEmailAttribute]  
  38. [DataType(DataType.EmailAddress)]  
  39. public string Email   
  40. {  
  41.     get;  
  42.     set;  
  43. }  
  44.   
  45. [Display("Full Name")]  
  46. public string FullName  
  47. {  
  48.     get {  
  49.         return FirstName + " " + LastName;  
  50.     }  
  51. }  
Notice the property FullName. This is a simple concatenation of the FirstName and LastName fields. Now all we need to do it call that in our razor code. Here is the altered table code.
  1. @model IEnumerable  
  2. <ViewModelUser>  
  3.     <table class="table table-bordered">  
  4.         <thead>  
  5.             <tr>  
  6.                 <th>      
  7.       @Html.DisplayNameFor(model => model.UserName)      
  8.    </th>  
  9.                 <th>      
  10.       @Html.DisplayNameFor(model => model.FullName)      
  11.    </th>  
  12.                 <th>      
  13.       @Html.DisplayNameFor(model => model.Email)      
  14.    </th>  
  15.                 <th></th>  
  16.             </tr>  
  17.         </thead>  
  18.         <tbody>      
  19. @foreach (var item in Model)      
  20. {      
  21.   
  22.             <tr>  
  23.                 <td>      
  24.       @Html.DisplayFor(modelItem => item.UserName)      
  25.    </td>  
  26.                 <td>      
  27.       @Html.DisplayFor(modelItem => item.FullName)      
  28.    </td>  
  29.                 <td>      
  30.       @Html.DisplayFor(modelItem => item.Email)      
  31.    </td>  
  32.                 <td>  
  33.                     <input type="button" value="Edit" />  
  34.                     <input type="button" value="Delete" />  
  35.                 </td>  
  36.             </tr>      
  37. }      
  38.   
  39.         </tbody>  
  40.     </table>  
And that's all you have to do. You can use this approach with most ViewModel fields to make your razor code much more succinct and easier to write.Till next time .

Up Next
    Ebook Download
    View all
    Learn
    View all