Sunday, 20 October 2013

using partial view in mvc 3.0

First take controller name Emp


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcPartialView.EF;


namespace MvcPartialView.Controllers
{
    public class EmpController : Controller
    {
        //
        // GET: /Emp/
        test_dbEntities db = new test_dbEntities();
       
        public ActionResult Index()
        {
            return View();
        }
        public PartialViewResult ShowEmployee()
        {
            return PartialView(db.emps);
        }

    }
}
----------------------------------------------------------------------

and add view with partial view so it is user control like .aspx 
with name 

@model IEnumerable<MvcPartialView.EF.emp>

<table>
<tr>
<td style="background-color:Silver;color:Navy;">Emp Id</td>
<td style="background-color:Silver;color:Navy;">Emp Name</td>
<td style="background-color:Silver;color:Navy;">Emp age</td>
</tr>
@foreach (var item in Model)
{
  <tr>
<td style="background-color:Silver;color:Navy;">@item.id</td>
<td style="background-color:Silver;color:Navy;">@item.name</td>
<td style="background-color:Silver;color:Navy;">@item.age</td>
</tr>  
}
<tr>
<td style="background-color:Silver;color:Navy;">
    <input id="txt" type="text" /></td>
<td style="background-color:Silver;color:Navy;"></td>
<td style="background-color:Silver;color:Navy;"></td>
</tr>  
</table>

-------------------------------------------------------------------------
now goes to global.aspx

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Emp", action = "ShowEmployee", id = UrlParameter.Optional } // Parameter defaults
            );

        }

now run this it will give output like this:

Emp Id Emp Name Emp age
23 deepak 45
24 hari 35
32 abc 23
33 xyz 23
34 pqw 45
35 huj 45
36 bnm 45
37 ghf 45
38 qas 45
39 tyu 45
40 jkl 45
41 lop 45
42 der 67
-----------------------------------------------------------------------------
now we will  call this usercontrol in our index page;

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
    <script src="../../Scripts/jquery-1.5.1.js"
     type="text/javascript"></script>

     <script type="text/javascript">
         $(document).ready(function () { show(); });

         function show() {
             $.get('/Emp/ShowEmployee', null, function (data) { chk(data); });          
         }
         function chk(abc) {            
             $('#d1').html(abc);
         }

         function chk1() {
             alert($("#txt").val());
         }
     </script>

</head>
<body>

    <input id="Button1" type="button" onclick="chk1();" value="button" />

    <div id="d1" >
        
    </div>
</body>
</html>
------------------------------------------------------------
now change our global.aspx and change action index like this

 public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Emp", action = "index", id = UrlParameter.Optional } // Parameter defaults
            );

        }



No comments:

Post a Comment