Emp Game Model Class

In Web Config
  1. <connectionStrings>  
  2.     <add name="DOTNETEntities" providerName="System.Data.SqlClient" connectionString="Data Source=DHIRENDRAKUMAR;initial catalog=DOTNET;user id=sa;password=P@ssw0rd" />  
  3. </connectionStrings>  
  4. Add Class in Model public class DotNetDBDataContext : DbContext { public DotNetDBDataContext() : base("name=DOTNETEntities") { } } public class EmployeeGames { public int EmpGameId { getset; } public string EmpName { getset; } public string Games { getset; } } Add Controller public class EmpGamesController : Controller { DotNetDBDataContext EmpGameContext = new DotNetDBDataContext(); List  
  5. <EmployeeGames> EmpGame = null// GET: EmpGames public ActionResult Index() { EmpGame = EmpGameContext.Database.SqlQuery  
  6.     <EmployeeGames>("SP_GetEmployeeGames").ToList(); ViewBag.EmpGamesData = EmpGame; return View(); } public ActionResult AddGames(int Id) { EmployeeGames Empobj = new EmployeeGames(); Empobj = EmpGameContext.Database.SqlQuery  
  7.         <EmployeeGames>("SP_GetParticularEmployeeGames @EmpGameId"new SqlParameter("EmpGameId", Id)).FirstOrDefault(); ViewBag.EmpGamesData = Empobj; return View(); } [HttpPost] public ActionResult UpdateGames(EmployeeGames EmpGames) { bool isSuccess=falseint result; result = EmpGameContext.Database.SqlQuery  
  8.             <int>("SP_UpdateEmployeeGames @EmpGameId,@Games"new SqlParameter("EmpGameId", EmpGames.EmpGameId), new SqlParameter("Games", EmpGames.Games)).FirstOrDefault  
  9.                 <int>(); if (result > 0) isSuccess = truereturn new JsonResult { Data = isSuccess, JsonRequestBehavior = JsonRequestBehavior.AllowGet }; } } Add View @{ Layout = null; }  
  10.                     <!DOCTYPE html>  
  11.                     <html>  
  12.   
  13.                     <head>  
  14.                         <meta name="viewport" content="width=device-width" />  
  15.                         <title>Index</title>  
  16.                         <script type="text/javascript">  
  17.                             function AddMoreGame(EmpGameId)  
  18.                             {  
  19.                                 //var url [email protected]("AddGames", "EmpGames") +"?Id=" + EmpGameId;  
  20.                                 window.location.href = "AddGames?Id=" + EmpGameId;  
  21.                                 // window.location.href = url;  
  22.                             }  
  23.                         </script>  
  24.                     </head>  
  25.   
  26.                     <body>  
  27.                         <div>  
  28.                             <table>  
  29.                                 <tr>  
  30.                                     <td headers="Employee Name"></td>  
  31.                                     <td headers=" Games Played"></td>  
  32.                                     <td headers="Action"></td>  
  33.                                 </tr>  
  34.                                 @foreach (var Item in ViewBag.EmpGamesData)   
  35.                                 {  
  36.                                 <tr>  
  37.                                     <td>@Item.EmpName</td>  
  38.                                     <td>@Item.Games</td>  
  39.                                     <td><a href="#" onclick="AddMoreGame(@Item.EmpGameId)">Add</a></td>  
  40.                                 </tr>  
  41.                                 }  
  42.                             </table>  
  43.                         </div>  
  44.                     </body>  
  45.   
  46.                     </html>  
  47.                     Add AddGames View @model ManualEmpGames.Models.EmployeeGames @{ Layout = null; } @{ ManualEmpGames.Models.EmployeeGames obj = new ManualEmpGames.Models.EmployeeGames(); if (ViewBag.EmpGamesData != null) { obj = ViewBag.EmpGamesData; } }  
  48.                     <!DOCTYPE html>  
  49.                     <html>  
  50.   
  51.                     <head>  
  52.                         <meta name="viewport" content="width=device-width" />  
  53.                         <title>AddGames</title>  
  54.                         <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>  
  55.                         <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>  
  56.                         <script>  
  57.                             $(document).ready(function()  
  58.                                {  
  59.                                 var iCnt = 0;  
  60.                                 // CREATE A "DIV" ELEMENT AND DESIGN IT USING JQUERY ".css()" CLASS.  
  61.                                 var container = $(document.createElement('div')).css({  
  62.                                     padding: '5px',  
  63.                                     margin: '20px',  
  64.                                     width: '170px',  
  65.                                     border: '1px dashed',  
  66.                                     borderTopColor: '#999',  
  67.                                     borderBottomColor: '#999',  
  68.                                     borderLeftColor: '#999',  
  69.                                     borderRightColor: '#999'  
  70.                                 });  
  71.                                 $('#btAdd').click(function()  
  72.                                  {  
  73.                                     for (var iCnt = 0; iCnt < parseInt(document.getElementById('txtTotalGame').value); iCnt++)  
  74.                                     {  
  75.                                         // ADD TEXTBOX.  
  76.                                         $(container).append('<input type=text class="input" id=tb' + iCnt + ' value="" placeholder="Enter Game Name"/><input type="button" id="btRemove" value="Remove Element" class="bt" />');  
  77.                                         // SHOW SUBMIT BUTTON IF ATLEAST "1" ELEMENT HAS BEEN CREATED.  
  78.                                         if (iCnt == 1) {  
  79.                                             var divSubmit = $(document.createElement('div'));  
  80.                                             $(divSubmit).append('<input type=button class="bt" onclick="GetTextValue()" id=btSubmit value=Submit />');  
  81.                                         }  
  82.                                         // ADD BOTH THE DIV ELEMENTS TO THE "main" CONTAINER.  
  83.                                         $('#main').after(container, divSubmit);  
  84.                                     }  
  85.                                     // AFTER REACHING THE SPECIFIED LIMIT, DISABLE THE "ADD" BUTTON.  
  86.                                     // (20 IS THE LIMIT WE HAVE SET)  
  87.                                 });  
  88.                                 // REMOVE ELEMENTS ONE PER CLICK.  
  89.                                 $('#btRemove').click(function()  
  90.                                  {  
  91.                                     if (iCnt != 0)  
  92.                                     {  
  93.                                         $('#tb' + iCnt).remove();  
  94.                                         iCnt = iCnt - 1;  
  95.                                     }  
  96.                                     if (iCnt == 0)  
  97.                                     {  
  98.                                         $(container)  
  99.                                             .empty()  
  100.                                             .remove();  
  101.                                         $('#btSubmit').remove();  
  102.                                         $('#btAdd')  
  103.                                             .removeAttr('disabled')  
  104.                                             .attr('class''bt')  
  105.                                     }  
  106.                                 });  
  107.                             });  
  108.                             // PICK THE VALUES FROM EACH TEXTBOX WHEN "SUBMIT" BUTTON IS CLICKED.  
  109.                             var divValue, values = '';  
  110.   
  111.                             function GetTextValue()  
  112.                             {  
  113.                                 $(divValue)  
  114.                                     .empty()  
  115.                                     .remove();  
  116.                                 values = '';  
  117.                                 $('.input').each(function()  
  118.                                 {  
  119.                                     divValue = values += this.value + ','  
  120.                                 });  
  121.                                 debugger;  
  122.                                 var Employee =  
  123.                                 {  
  124.                                     EmpGameId: '@obj.EmpGameId',  
  125.                                     Games: '@obj.Games' + divValue  
  126.                                 }  
  127.                                 $.ajax({  
  128.                                     url: '@Url.Action("UpdateGames", "EmpGames")',  
  129.                                     type: 'POST',  
  130.                                     data:  
  131.                                     {  
  132.                                         EmpGames: Employee  
  133.                                     },  
  134.                                     dataType: 'json',  
  135.                                     cache: false,  
  136.                                     success: function(responce)   
  137.                                     {  
  138.                                         if (responce != null && responce == true)  
  139.                                         {  
  140.                                             var url = "Index";  
  141.                                             window.location.href = url;  
  142.                                         }  
  143.                                     }  
  144.                                 });  
  145.                                 $(divValue).append('<p><b>Your selected values</b></p>' + values);  
  146.                                 $('body').append(divValue);  
  147.                             }  
  148.                         </script>  
  149.                     </head>  
  150.   
  151.                     <body>  
  152.                         @{  
  153.                         <div>  
  154.                             <table style="border:double">  
  155.                                 <tr>  
  156.                                     <td>@obj.EmpName</td>  
  157.                                     <td></td>  
  158.                                     <td></td>  
  159.                                 </tr>  
  160.                             </table>  
  161.                             <div id="main">  
  162.                                 <input type="text" id="txtTotalGame" value="0" placeholder="Enter How Many Games" />  
  163.                                 <input type="button" id="btAdd" value="Add Element" class="bt" />  
  164.                             </div>  
  165.                         </div>  
  166.                         }  
  167.                     </body>  
  168.   
  169.                     </html>