Hi,
I have created a pagedlist in mvc view. But now when i do pagination my model object properties lost on 2nd page and after pages. I am creating a report page on filter basis. So what is the best process to achieve this ?
What I have tried:
Here is my Model class
public partial class log_master
{
[Key]
public long log_id { get; set; }
[Display(Name ="User")]
public string logged_user { get; set; }
[Display(Name ="Process Name")]
public string controller_name { get; set; }
[Display(Name ="Action Name")]
public string action_name { get; set; }
[Display(Name ="Task")]
public string log_msg { get; set; }
[Display(Name ="DateTime")]
public Nullable<System.DateTime> log_time { get; set; }
public string log_route { get; set; }
public int? PageCount { get; set; }
public int? PageNumber { get; set; }
}
Here is my view model class for filtration
public class LogMasterViewModel
{
[Key]
public int lmvm_id { get; set; }
[Display(Name = "Personal Number")]
public string pNumber { get; set; }
[Display(Name = "Process Name")]
public string ProcessName { get; set; }
[Display(Name = "From"), DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true), Required(ErrorMessage = "Please Select From Date")]
public Nullable<System.DateTime> FromDate { get; set; }
[Display(Name = "To"), DataType(DataType.Date), DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true), Required(ErrorMessage = "Please Select To Date")]
public Nullable<System.DateTime> ToDate { get; set; }
public IPagedList<log_master> LogList { get; set; }
}
I have created my controller like this.
// GET: Admin/LogReport
public ActionResult Index(LogMasterViewModel lmvm, int? Page_no)
{
TestEntities db = new Models.TestEntities();
if (Session["loginid"] == null)
{
return RedirectToAction("Index", "Login", new { area = "" });
}
try
{
ViewBag.ProcessList = new SelectList(GetLogProcess() as List<SelectListItem>, "Value", "Text");
if (lmvm.FromDate.HasValue && lmvm.ToDate.HasValue)
{
int pageSize = 10;
int pageIndex = 1;
pageIndex = Page_no.HasValue ? Convert.ToInt32(Page_no) : 1;
var result = (from lm in db.log_master
where DbFunctions.TruncateTime(lm.log_time) >= DbFunctions.TruncateTime(lmvm.FromDate) &&
DbFunctions.TruncateTime(lm.log_time) <= DbFunctions.TruncateTime(lmvm.ToDate)
select lm).ToList();
if (!string.IsNullOrEmpty(lmvm.pNumber))
result = result.Where(x => x.logged_user.ToUpper() == lmvm.pNumber.ToUpper()).ToList();
if (!string.IsNullOrEmpty(lmvm.ProcessName))
result = result.Where(x => x.controller_name.ToUpper() == lmvm.ProcessName.ToUpper()).ToList();
lmvm.LogList = result.ToPagedList(pageIndex, pageSize);
if (!Page_no.HasValue)
DBHelper.InsertLog(Convert.ToString(Session["loginid"]), "LogReport", "Index-Get", "Display Report-" + sbfilter.ToString());
}
}
catch (Exception ex)
{
DBHelper.InsertErrorLog(Convert.ToString(Session["loginid"]), "LogReport", "Index-Get", ex.HResult.ToString(), ex.InnerException != null ? ex.InnerException.Message.ToString() : ex.Message.ToString());
}
return View(lmvm);
}
My view is
@model INBA.Models.LogMasterViewModel
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" />
@{
ViewBag.Title = "Index";
Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml";
}
<div style="width:90%; margin:10px auto;">
<h5>User Activity Report</h5>
<hr />
@using (Html.BeginForm("Index", "LogReport", FormMethod.Get))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="table-responsive">
<table class="table table-borderless">
<tr>
<td>
@Html.LabelFor(model => model.pNumber, htmlAttributes: new { @class = "control-label col-md-12" })
</td>
<td>
@Html.EditorFor(model => model.pNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.pNumber, "", new { @class = "text-danger" })
</td>
<td>
@Html.LabelFor(model => model.ProcessName, htmlAttributes: new { @class = "control-label col-md-12" })
</td>
<td>
@Html.DropDownListFor(model => model.ProcessName, (SelectList)ViewBag.ProcessList, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProcessName, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td>
@Html.LabelFor(model => model.FromDate, htmlAttributes: new { @class = "control-label col-md-12" })
</td>
<td>
@Html.EditorFor(model => model.FromDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FromDate, "", new { @class = "text-danger" })
</td>
<td>
@Html.LabelFor(model => model.ToDate, htmlAttributes: new { @class = "control-label col-md-12" })
</td>
<td>
@Html.EditorFor(model => model.ToDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ToDate, "", new { @class = "text-danger" })
</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td>
<input type="submit" value="Get Report" class="btn btn-default" />
<input type="submit" value="Download Report" formaction="ExportToExcel" formmethod="post" class="btn btn-default" />
</td>
</tr>
</table>
</div>
</div>
<hr />
<div id="content_table" class="table-responsive">
@if (Model.LogList != null)
{
<table class="table table-bordered table-striped">
<thead>
<tr class="table-primary" style="white-space:nowrap;">
<td>Logged User</td>
<td>Process</td>
<td>Action</td>
<td>DateTime</td>
<td>Activity</td>
</tr>
</thead>
@if (Model.LogList.Count > 0)
{
foreach (var item in Model.LogList)
{
<tr>
<td> @item.logged_user </td>
<td> @item.controller_name </td>
<td> @item.action_name </td>
<td> @item.log_time </td>
<td> @item.log_msg </td>
</tr>
}
}
</table>
<div style="text-align:center;">
Page @(Model.LogList.PageCount < Model.LogList.PageNumber ? 0 : Model.LogList.PageNumber) of @Model.LogList.PageCount
@Html.PagedListPager(Model.LogList, Page => Url.Action("Index", new { Page_no = Page, Model.FromDate, Model.ToDate, Model.pNumber, Model.ProcessName }))
Showing @Model.LogList.FirstItemOnPage to @Model.LogList.LastItemOnPage of @Model.LogList.TotalItemCount
</div>
}
</div>
}
</div>