Gcobani Mkontwana

Gcobani Mkontwana

  • 1.4k
  • 480
  • 15k

How to access members of your model to your Controller?

Feb 9 2020 11:49 PM
  1. // Index.cshtml  
  2.   
  3.   
  4. @{  
  5.     ViewBag.Title = "EventManagement List";  
  6. }  
  7. <hr/>  
  8. <hr/>  
  9. <hr/>  
  10. <h2>EventManagement List</h2>  
  11. <table id="EventManagementTable" class="display">  
  12.     @*Semantic UI*@  
  13.     @*<table id="employeeTable" class="ui celled table">*@  
  14.     @*Bootstrap*@  
  15.     @*<table id="employeeTable" class="table table-striped table-bordered">*@  
  16.   
  17.     <thead>  
  18.         <tr>  
  19.             <th>TrainingID</th>  
  20.             <th>TrainingType</th>  
  21.             <th>TrainingDescription</th>  
  22.             <th>Price</th>  
  23.             <th>Venue</th>  
  24.             <th>Facilitator</th>  
  25.             <th>WhoAttend</th>  
  26.             <th>RSVP</th>  
  27.         </tr>  
  28.     </thead>  
  29.     <tfoot>  
  30.         <tr>  
  31.             <th>TrainingID</th>  
  32.             <th>TrainingType</th>  
  33.             <th>TrainingDescription</th>  
  34.             <th>Price</th>  
  35.             <th>Venue</th>  
  36.             <th>Facilitator</th>  
  37.             <th>WhoAttend</th>  
  38.             <th>RSVP</th>  
  39.         </tr>  
  40.     </tfoot>  
  41.   
  42. </table>  
  43.   
  44.   
  45. <!--Normal DataTables-->  
  46. <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.20/datatables.min.css" />  
  47.   
  48. <!---JQuery ThemeRoller-->  
  49. <link href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />  
  50. <link href="https://cdn.datatables.net/1.10.15/css/dataTables.jqueryui.min.css" rel="stylesheet" />  
  51.   
  52. <!--Semantic UI-->  
  53. <link href="//cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.6/semantic.min.css" rel="stylesheet" />  
  54. <link href="https://cdn.datatables.net/1.10.15/css/dataTables.semanticui.min.css" rel="stylesheet" />  
  55.   
  56. <!-- Bootstrap 4 -->  
  57. <link href="https://cdn.datatables.net/1.10.15/css/dataTables.bootstrap4.min.css" rel="stylesheet" />  
  58.   
  59. @section scripts{  
  60.       
  61.     <script src="//cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script>  
  62.     <script src="https://cdn.datatables.net/1.10.15/js/dataTables.jqueryui.min.js"></script>  
  63.     <script src="https://cdn.datatables.net/1.10.15/js/dataTables.semanticui.min.js"></script>  
  64.     <link href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0.0-alpha.6/css/bootstrap.css" rel="stylesheet"/>  
  65.     <script src="https://cdn.datatables.net/1.10.15/js/dataTables.bootstrap4.min.js"></script>  
  66.   
  67.     <script>  
  68.   
  69.      $(document).ready(function () {  
  70.   
  71.             $("#EventManagementTable").DataTable(  
  72.                 {  
  73.                     "ajax": {  
  74.                         "url""/EventManagement/GetList",  
  75.                         "type""GET",  
  76.                         "datatype""json"  
  77.                     },  
  78.                     "columns": [  
  79.                         { "data""TrainingID" },  
  80.                         { "data""TrainingType" },  
  81.                         { "data""TrainingDescription" },  
  82.                         { "data""Price" },  
  83.                         { "data""Facilitator" },  
  84.                         { "data""WhoAttend" },  
  85.                         {"data":"RSVP"}  
  86.                     ]  
  87.   
  88.                 });  
  89.   
  90.         });  
  91.     </script>  
  92.   
  93.       
  94.     }  
Hi Team
 
I want to find a way to access my members of model class to my Controller class, when using ApplicationDbContext. The database is attached to my App_Data and has table to it, and want to find a way to access those fields in my Controller class using ApplicationDbContext, can anyone help me if there is a way. Below is my logic so far and screen shot showing what i am aiming to achieve.
 
  1. //Controller class  
  2.   
  3. using System.Web;  
  4. using System.Web.Mvc;  
  5. using ContentManagementSystem.Filters;  
  6. using ContentManagementSystem.Models;  
  7. using Microsoft.AspNet.Identity.EntityFramework;  
  8.   
  9. namespace ContentManagementSystem.Controllers  
  10. {  
  11.     public class DashboardController : Controller  
  12.     {  
  13.          
  14.         public ActionResult _Index()  
  15.         {  
  16.   
  17.             return View();  
  18.         }  
  19.   
  20.         public ActionResult GetList()  
  21.         {  
  22.             using(ApplicationDbContext db = new ApplicationDbContext())  
  23.             {  
  24.                 var emptList = db.Users.  
  25.                 return Json(new { data = emp })  
  26.   
  27.             }  
  28.         }  
  29.           
  30.     }  
  31. }  
  32.   
  33. // Model class  
  34.   
  35. using System;  
  36. using System.Collections.Generic;  
  37. using System.Linq;  
  38. using System.Web;  
  39. using Microsoft.AspNet.Identity.EntityFramework;  
  40.   
  41. namespace ContentManagementSystem.Models  
  42. {  
  43.     public class ApplicationUser:IdentityUser  
  44.     {  
  45.         public string Email { getset; }  
  46.         public bool ConfirmedEmail { getset; }  
  47.   
  48.     }  
  49.   
  50.     public class ApplicationDbContext: IdentityDbContext  
  51.     {  
  52.         public ApplicationDbContext()  
  53.             : base("eNtsaOnlineRegistrationDB")  
  54.         {  
  55.   
  56.         }  
  57.     }  

Answers (16)