Wednesday, 21 October 2015

MVC : Calling the Partial View on Selected Index changed


@model MVCDemo.Models.StudentDetails
@{
    ViewBag.Title = "Index";
}

<script type="text/javascript" src="js/script.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>


<script type="text/javascript">
   $(document).ready(function () {
        $(document).ready(function () {
            $("#myDropDown").change(function () {
                var selectedID = $(this).val();
                alert(selectedID);
                var categoryId = $("#myDropDown").val();
                $("#dvCategoryResults").load('@(Url.Action("SelectStudent", "GetStudents", null, Request.Url.Scheme))?selectedStudent=' + selectedID);
            });
        });

    });
</script>




<style>
    table, td, th {
        border: 1px solid green;
        border-collapse: collapse;
    }

    th {
        border: 1px solid black;
        background-color: green;
        color: white;
    }
</style>

<br><br><br><br><br><br>
@Html.DropDownListFor(M => M.Studentmodel, new SelectList(Model.Studentmodel, "ID", "Name",0),"Please Select", new { @id = "myDropDown" })



<br>

<div id="dvCategoryResults">
    @Html.Partial("~/Views/Shared/_PartialStudent.cshtml", Model);
</div>


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

namespace Student_Application.Controllers
{
    public class GetStudentsController : Controller
    {
        // GET: GetStudents
        public ActionResult Index()
        {
            StudentDetails _objuserloginmodel = new StudentDetails();
            List<StudentDetail> _objStudent = new List<StudentDetail>();
            _objStudent = _objuserloginmodel.GetStudentList();
            _objuserloginmodel.Studentmodel = _objStudent;
            return View(_objuserloginmodel);
        }

        [HttpGet]
        public ActionResult SelectStudent(int? selectedStudent)
        {
            StudentDetails _objuserloginmodel = new StudentDetails();
            List<StudentDetail> _objStudent = new List<StudentDetail>();
            _objStudent = _objuserloginmodel.GetStudentList(selectedStudent);
            _objuserloginmodel.Studentmodel = _objStudent;

            return PartialView("_PartialStudent", _objuserloginmodel);

        }

    }
}



Partial View


@model MVCDemo.Models.StudentDetails


@if (Model.Studentmodel != null && Model.Studentmodel.Count() > 0)
{



    var grid = new WebGrid(Model.Studentmodel, canSort: false);

    <div>
        @grid.GetHtml(columns:
                                grid.Columns
                                (
                                    grid.Column("ID", "Stud ID"),
                                    grid.Column("Name", "Stud Name"),
                                    grid.Column("Class", "Class"),
                                    grid.Column("Section", "Section"),
                                    grid.Column("Address", "Address")
                                ), mode: WebGridPagerModes.Numeric)
    </div>

}




No comments:

Post a Comment