Saturday, 11 July 2015

Ajax Call to controller and view

Create.cshtml
------------------------------------------------------------


@model MvcApplication1.Models.student

@{
    ViewBag.Title = "Create";
}
<!-- for popup -->
    <div id="dvPop" style="display:none;position:absolute;top:100px;left:100px;z-index:1000;height:400px;width:400px;background-color:red"></div>
<!-- End for popup -->



<h2>Create</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>student</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.FirstName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.FirstName)
            @Html.ValidationMessageFor(model => model.FirstName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.LastName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.LastName)
            @Html.ValidationMessageFor(model => model.LastName)
        </div>

        <p>
            <input type="submit" value="Create" />
           
            <input type="button" value="ShowList" onclick="showStudList();" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
 <script lang="javascript">
     function showStudList() {

       
        // $(this).load("/StudentName/index");

         $.ajax({
             type: "GET",
             url: "/StudentName/index",
             data: {},
             success: function (viewHTML) {
                 document.getElementById('dvPop').style.display = '';
                 $("#dvPop").html(viewHTML);
             },
             error: function (errorData) { onError(errorData); }
         });

     }

</script>




----------------

StudentNameController.cs
--------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class StudentNameController : Controller
    {
        //
        // GET: /StudentName/

        private adiEntities db = new adiEntities();

        public ActionResult Index()
        {
            //Get student List
            List<student> studList = db.students.Where(x=>x.FirstName=="Ram").ToList();
            return PartialView("Index", studList);
        }

    }
}

No comments:

Post a Comment