Razor With Angular Cocktail

Hi guys, have you ever tried mixing Angular JS with Razor? You may not need it? Ok but you may get benefits from it. Let’s take a look.

In this listing we have MVC view written in Razor with HTML tags and here we may add Angular dressing to it.

  1. @model IEnumerable<EmployeeRepository.Models.Department>  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6. <script src="~/APPSCRIPTS/angular.min.js"></script>  
  7. <h2>Index</h2>  
  8.   
  9. <p>  
  10.     @Html.ActionLink("Create New", "Create")  
  11. </p>  
  12.   
  13. <div ng-app="NGapp" ng-controller="NGcontroller">  
  14.     <script>  
  15.         var app = angular.module("NGapp", []);  
  16.         app.controller("NGcontroller", function ($scope) {  
  17.   
  18.             $scope.depts = [];  
  19.                           
  20.   
  21.             @foreach(var element in Model)  
  22.             {  
  23.                 <text> $scope.depts.push({ id: '@element.DepartmentID',name: '@element.DepartmentName'});  </text>  
  24.             }  
  25.   
  26.         });  
  27.   
  28.     </script>  
  29.     Search Departments: <input type="text" ng-model="searchDepartment" /><br />  
  30.     <br />  
  31.     <table class="table">  
  32.         <tr>  
  33.             <th>  
  34.                 @Html.DisplayNameFor(model => model.DepartmentName)  
  35.             </th>  
  36.             <th></th>  
  37.         </tr>  
  38.         <tr ng-repeat="dept in depts | filter:searchDepartment">  
  39.             <td>{{dept.name}}</td>  
  40.             <td>  
  41.                 <a href="@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()/Edit/{{dept.id}}">Edit</a> |  
  42.                 <a data-ng-href="@Url.Action("Details", "Department")/{{dept.id}}" >Details </a> |                
  43.                 <a data-ng-href="@Url.Action("Delete", "Department")/{{dept.id}}">Delete </a>  
  44.             </td>  
  45.         </tr>  
  46.   
  47.     </table>  
  48.      
  49.   
  50.     <hr />  
  51.     <table class="table">  
  52.         <tr>  
  53.             <th>  
  54.                 @Html.DisplayNameFor(model => model.DepartmentName)  
  55.             </th>  
  56.             <th></th>  
  57.         </tr>  
  58.          
  59.         @foreach (var item in Model)  
  60.         {  
  61.             <tr>  
  62.                 <td>  
  63.                     @Html.DisplayFor(modelItem => item.DepartmentName)  
  64.                 </td>  
  65.                 <td>  
  66.                     @Html.ActionLink("Edit", "Edit", new { id = item.DepartmentID }) |  
  67.                     @Html.ActionLink("Details", "Details", new { id = item.DepartmentID }) |  
  68.                     @Html.ActionLink("Delete", "Delete", new { id = item.DepartmentID })  
  69.                 </td>  
  70.             </tr>  
  71.         }  
  72.   
  73.     </table>  
  74.       
  75. </div>  
The first highlighted block: 
  1. @foreach(var element in Model)  
  2.             {  
  3.                 <text> $scope.depts.push({ id: '@element.DepartmentID',name: '@element.DepartmentName'});  </text>  
  4.             }  
  5.   
  6.         });  
Using foreach clause of Razor we iterate through Model (the list of departments), in each iteration we use push method to add department id and name to Angular array object.
 
And we use HTML text input used as a filter using ng-model="searchDepartment"
  1. Search Departments: <input type="text" ng-model="searchDepartment" /><br />  
And last we have this piece of code to add links and list departments exactly as Razor does.
  1. <tr ng-repeat="dept in depts | filter:searchDepartment">  
  2.     <td>{{dept.name}}</td>  
  3.     <td>  
  4.         <a href="@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()/Edit/{{dept.id}}">Edit</a> |  
  5.         <a data-ng-href="@Url.Action("Details", "Department")/{{dept.id}}" >Details </a> |                
  6.         <a data-ng-href="@Url.Action("Delete", "Department")/{{dept.id}}">Delete </a>  

Using ng-repeat with filter expression to list department names and then a column for the action links, notice here, we use two different kinds of getting the controller name.

  1. @HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()/Edit/{{dept.id}}">Edit  

This is a way to get the controller name and below is another one.

  1. <a data-ng-href="@Url.Action("Details", "Department")/{{dept.id}}" >Details  
I see the second one is pretty easier and of course; it is up to you.
 
Note - Using this way; Angular with Razor you may avoid what is so-called (circular reference exception) if you have master-details relationship scaffolded by MVC, you may have employees in each department, so keep attention if you use a reference to (department.employees) in your code. In this view you don’t need to have a reference to a list of employees inside the department, but by using department ID , you may get another view display this list of employees or even another Angular array to load this list.