Sunday, April 28, 2013

Read And Write data in MVC using n tier architecture

In this scenario we discuss how we can do CRUD operations in MVC using n tier architecture. in this lesson i'm going to assume that reader must have basic knowledge of MVC framework and how it works.

 <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml">  
 <head runat="server">  
   <title></title>  
    <script type="text/javascript" src="../../Scripts/jquery-1.5.1.min.js"></script>  
 </head>  
 <body>  
   <div>  
     <%: Html.ActionLink("Create full time student", "CreatefullStudent")%><br>  
     <%: Html.ActionLink("Create part time student", "CreatepartStudent")%>  
     <br />  
     <input type="button" value="Get student" id="btnstudent" />  
     <div id="studentlist"></div>  
   </div>  
 </body>  
 </html>  
 <script type="text/javascript">  
   $(document).ready(function () {  
     $("#btnstudent").click(function () { GetStudent(); });  
     GetStudent();  
   });  
   function GetStudent() {  
     $.ajax({  
       url: "Home/GetStudentList",  
       type: 'GET',  
       contentType: 'application/json; charset=utf-8',  
       dataType: 'json',  
       success: function (result) {  
         alert("test");  
         if (result.length == 0) {  
           $("#studentlist").html("No details available");  
         } else {  
           var str = "<table><thead><tr><td>Id</td><td>Name</td></tr></thead><tbody>"  
           for (var i = 0; i < result.length; i++) {  
             str += "<tr>";  
             str += "<td>" + result[i].Id + "</td>";  
             str += "<td>" + result[i].Name + "</td>";  
             str += "</tr>";  
           }  
           str += "</tbody></table>"  
           $("#studentlist").html(str);  
         }  
       }  
     });  
   }  
 </script>