Link to home
Start Free TrialLog in
Avatar of EdED
EdED

asked on

Paging in Partial View Using C# .Net, JQuery

I am developing a C# .net MVC page to allow users to make some selection, query data and display the data with paging function.
I use a partial view for the data display but fail to maintain the main view and the data after click on different page.

Please advise me, have attached the code as below (Index.cshtml, SearchController.cs, WebGrids.cshtml)

Index.cshtml
______________

@model DLTWeb.Models.SearchModel

@{
    ViewBag.Title = "Index";    
}

<header>
    <script src="~/Scripts/jquery-3.0.0.js"></script>
    <script src="~/Scripts/jquery-3.0.0.min.js"></script>
    <script src="~/Scripts/jquery-ui-1.8.24.min.js"></script>  
    <script src="~/Scripts/gridmvc.min.js"> </script>
    <link href="@Url.Content("~/Content/style.css")" rel="stylesheet" type="text/css" />  
    <link href="@Url.Content("~/Content/PagedList.css")" rel="stylesheet" type="text/css" />
</header>

<h2><font color="blue">@Html.Label("lblReport", "DLT Web Report")</font></h2>

<br />

<table>
    <tr>
        <td>Report Name</td>
        <td>@Html.DropDownListFor(m => m.ReportName, (IEnumerable<SelectListItem>)ViewBag.ListReport, "---------------Select Report----------------", new { @id = "ddlReport", @onchange = "javascript:GetFields(this.value);" })</td>        
    </tr>
    <tr>
        <td>Fields Name</td>
        <td>
            <select id="ddlFields" name="ddlFields">
                <option>---Select Fields---</option>
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            @Html.TextArea("taSearchValues", null, new { style="width: 200px; height: 60px; font-family: Times New Roman; font-size: 12px" } )
        </td>        
    </tr>
    <tr>
        <td colspan="2">
            <font font-family="verdana" color="red" size="1px">
                *Enter exact value, no wildcard characters allowed<br />
                *For multi search, separate each value with a carriage return key
            </font>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <div>
                <input type="submit" value="Submit" id="btnSubmit" />
            </div>
        </td>
    </tr>    
</table>

<div id="dvShow"></div>

<script language="javascript" type="text/javascript">
    function GetFields(_ReportType) {
        var procemessage = "<option value='0'> Please wait...</option>";
        $("#ddlFields").html(procemessage).show();
        var url = "/Search/GetSearchFields/";
               
        //$("#lblReport").val($("#ddlReport option:selected").text());
                       
        $.ajax({
            url: url,
            data: { iReportID: _ReportType },
            cache: false,
            type: "POST",
            success: function (data) {
                var markup = "<option value='0'>---Select Fields---</option>";
                for (var x = 0; x < data.length; x++) {
                    markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
                }
                $("#ddlFields").html(markup).show();
            },
            error: function (reponse) {
                alert("error : " + reponse);
            }
        });
    }

    $(document).ready(function () {  
        $("#btnSubmit").click(function () {  
            $.ajax({  
                url: '@Url.Action("WebGrids", "Search")',  
                type: 'GET',
                data: {
                    'strQueryType': 'Tile_ID',
                    'strValue': 'P7D04-5MU'
                },
                success: function (vHtml) {  
                            $("#dvShow").html("");  
                            $("#dvShow").html(vHtml);  
                },
                error: function (reponse) {
                    //alert("123");
                    alert("error : " + reponse);
                }
            });  
        });
    });

</script>


SearchController
________________

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DLTWeb.Models;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Functions;
using System.Web.Mvc;
using System.Web.Helpers;
using WebGridMvc;
using PagedList;
using PagedList.Mvc;

namespace DLTWeb.Controllers
{
    public class SearchController : Controller    
    {
        List<SearchModel> sm = new List<SearchModel>();

        public ActionResult Index()
        {            
            //List<SelectListItem> listItem = new List<SelectListItem>();
           
            sm = GetReportSearchFields();
            ViewBag.ListReport = new SelectList(sm, "ReportID", "ReportName");

            return View();
        }

        public List<SearchModel> GetReportSearchFields()
        {
            List<SearchModel> searchData = new List<SearchModel>();

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:56651/api/");

                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
               
                var Res = client.GetAsync("Search");
                Res.Wait();

                var result = Res.Result;

                if (result.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api  
                    var configResponse = result.Content.ReadAsStringAsync().Result;
                    searchData = JsonConvert.DeserializeObject<List<Models.SearchModel>>(CommonFunc.FixApiResponseString(configResponse));
                }

                return searchData;
            }
        }

        public ActionResult WebGrids(string strQueryType, string strValue, int? page)
        {
            List<RebelModel> searchData = new List<RebelModel>();
            int pageSize = 10;
            int pageIndex = 1;            

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:56651/api/");

                client.DefaultRequestHeaders.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                var Res = client.GetAsync("Dlt?strQueryType=" + strQueryType + "&strValue=" + strValue);
                Res.Wait();

                var result = Res.Result;

                if (result.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api  
                    var configResponse = result.Content.ReadAsStringAsync().Result;
                    searchData = JsonConvert.DeserializeObject<List<Models.RebelModel>>(CommonFunc.FixApiResponseString(configResponse));                    
                }

                return PartialView(searchData.ToList().ToPagedList(page ?? 1, pageSize));
            }

        }


WebGrids.cshtml
__________________

@using PagedList;
@using PagedList.Mvc;

@model IPagedList<DLTWeb.Models.RebelModel>

@{
    Layout = null;
}

<link href="@Url.Content("~/Content/PagedList.css")" rel="stylesheet" type="text/css" />

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<div id="Mypager">
@Html.PagedListPager(Model, page => Url.Action("WebGrids", new { page, strQueryType = "Tile_ID", strValue = "P7D04-5MU" }))
</div>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.First().Epi_Lot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().WF_MESLot)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().SaberLotId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().SaberMESLotId)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().FWT_lot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().FWT_Sublot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().DS1_Lot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().DS1_SubLot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Tile_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Die_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().GGI_Lot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().GGI_SubLot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().GSN_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().GGI_X)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().GGI_Y)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().XLoc)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().YLoc)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().FST_Lot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().FST_SubLot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().DS2_Lot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().DS2_SubLot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().TNR_Lot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().TNR_SubLot_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().TnR_Pos)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().TnR_Bin)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().TnR_Gsn)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Epi_Lot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.WF_MESLot)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SaberLotId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.SaberMESLotId)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FWT_lot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FWT_Sublot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DS1_Lot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DS1_SubLot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Tile_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Die_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.GGI_Lot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.GGI_SubLot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.GSN_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.GGI_X)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.GGI_Y)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.XLoc)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.YLoc)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FST_Lot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.FST_SubLot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DS2_Lot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DS2_SubLot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TNR_Lot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TNR_SubLot_ID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TnR_Pos)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TnR_Bin)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TnR_Gsn)
        </td>
    </tr>
}

</table>

@Html.PagedListPager(Model, page => Url.Action("WebGrids", new { page, strQueryType = "Tile_ID", strValue = "P7D04-5MU" }))
Avatar of EdED
EdED

ASKER

I manage to find out the solution myself, by using ajax query so that it wont refresh the whole page.

 $(document).ready(function () {      
        $("#btnSubmit").click(function () {            
            $.ajax({  
                url: '@Url.Action("WebGrids", "Search")',  
                type: 'GET',
                data: {
                    'strQueryType': $("#ddlFields").find("option:selected").text(),
                    'strValue': $("#taSearchValues").text()
                },
                success: function (vHtml) {  
                            $("#dvShow").html("");  
                            $("#dvShow").html(vHtml);  
                },
                error: function (reponse) {
                    alert("error : " + reponse);
                }
            });  
        });
    });
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.