Grid MVC paging not working

1.4k Views Asked by At

https://gridmvc.codeplex.com/

Here is the functionality i want to achieve, there should be a Text Box which takes reference number and on submitting, Grid should show records of that particular reference number. By Default when grid loads at first time, there should be no record . when Show All button is pressed grid shows all the records in the database.

I have achieved all but problem is the when i search particular reference number it brings up records of that particular number but paging does not work properly. it shows proper number of pages but when i click on lets say page number "2" it gets all the records on page number 2 instead of that particular reference number.

View

@using GridMvc.Html
@using GridMvc.Sorting
@model IEnumerable<GridMvc.Models.Logging>



<div  style="padding-top:50px;padding-left:15px;padding-right:15px">

@using (Html.BeginForm("Index", "Home"))
{
    <label>Enter Customer Reference Number</label>

    <div>
        <input type="text" value="" style="width:100px" name="subno"/>            
        <div style="margin-top:5px"></div>
        <table>
            <tr>
                <td><input type="submit" value="Search" name="Submit" /> </td>
                <td style="padding-left:5px"><input type="submit" value="Show All" name="Clear" /></td>
            </tr>
        </table>            
    </div>
        <div style="margin-top:15px"></div>

    @Html.Grid(Model).Columns(columns =>
        {
            /* Adding "OrderID" column: */

            columns.Add(o => o.ID)
                   .Titled("Number")
                   .SetWidth(100)
                    .Css("csstd");  
            columns.Add(o => o.Date, "Date")
                    .Titled("Date")
                    .SortInitialDirection(GridSortDirection.Descending)
                    .Format("{0:dd/MM/yyyy}")
                    .SetWidth(110)
                    .Css("csstd");                
            columns.Add(o => o.Time, "Time")
                    .Titled("Time")                
                    .SetWidth(110)
                    .Css("csstd");           
            columns.Add(o => o.Type)
                   .Titled("Type")
                   .SetWidth(150)
                   .Css("csstd")   
                   .ThenSortByDescending(o => o.ID);               
            columns.Add(o => o.Description)
                    .Css("csstd")         
                   .Titled("Description")
                   .SetWidth(250);
            columns.Add(o => o.Reference)
                    .Css("csstd")
                    .Titled("MSISDN")
                    .SetWidth(150);
            columns.Add(o => o.Response)
                    .Css("csstd")
                    .Titled("Response")
                    .SetWidth(150);                




        }).WithPaging(15).Sortable().Filterable().WithMultipleFilters()

}

</div>

Controller

  public ActionResult firstRun()
{
    return View("Index", new List<GridMvc.Models.Logging>());
}

public ActionResult Index(FormCollection form)
{
    wmas_subsEntities entitymodel = new wmas_subsEntities();

    string subno = form["subno"];

    List<GridMvc.Models.Logging> Logs = new List<Models.Logging>();

    if (form["Clear"] != null)
    {
        foreach (var item in entitymodel.Loggings.ToList())
        {
            DateTime ConvertedTime = GetConvertedTime(DateTime.Parse(item.DateTime.ToString()));

            Logs.Add(new Models.Logging()
            {
                Date = ConvertedTime,
                Description = item.Description,
                ID = item.ID,
                Reference = item.Reference,
                Response = item.Response,
                Time = String.Format("{0:h:mm tt}", ConvertedTime),
                Type = item.Type
            });
        }
    }
    else if (subno != null)
    {
        var items = from p in entitymodel.Loggings where p.Reference.Contains(subno) select p;
        if (items.Count() > 0)
        {
            foreach (var Childitem in items)
            {
                DateTime ConvertedTime = GetConvertedTime(DateTime.Parse(Childitem.DateTime.ToString()));

                Logs.Add(new Models.Logging()
                {
                    Date = ConvertedTime,
                    Description = Childitem.Description,
                    ID = Childitem.ID,
                    Reference = Childitem.Reference,
                    Response = Childitem.Response,
                    Time = String.Format("{0:h:mm tt}", ConvertedTime),
                    Type = Childitem.Type
                });
            }
        }
    }
    else
    {
        foreach (var Childitem in entitymodel.Loggings)
        {
            DateTime ConvertedTime = GetConvertedTime(DateTime.Parse(Childitem.DateTime.ToString()));

            Logs.Add(new Models.Logging()
            {
                Date = ConvertedTime,
                Description = Childitem.Description,
                ID = Childitem.ID,
                Reference = Childitem.Reference,
                Response = Childitem.Response,
                Time = String.Format("{0:h:mm tt}", ConvertedTime),
                Type = Childitem.Type
            });

        }
    }

    return View(Logs);
}

private DateTime GetConvertedTime(DateTime dateTime)
{
    DateTime utcDateTime = dateTime.ToUniversalTime();

    string nzTimeZoneKey = "Pakistan Standard Time";
    TimeZoneInfo nzTimeZone = TimeZoneInfo.FindSystemTimeZoneById(nzTimeZoneKey);
    DateTime nzDateTime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, nzTimeZone);

    return nzDateTime;
}

Login Controller

 [HttpPost]

public ActionResult Login(LoginModel model, string returnUrl)
{
    if (ModelState.IsValid && AuthorizeUser(model.UserName, model.Password))
    {
        Session["sessionUserId"] = model.UserName;

        return RedirectToAction("firstRun", "Home");
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}
0

There are 0 best solutions below