MVC partial view grid with working paging and shorting

#region EmployeeWorkReport Controler Page Coding
[HttpGet]
public ActionResult EmployeeWorkReport()
{
using (DataAccessLayer.DatabaseContext db = new DataAccessLayer.DatabaseContext())
{
ViewBag.LoginID = new SelectList(db.Employees.Where(_ => _.status.Equals(“1”) && _.Role.Equals(“Employee”)).Select(_ => new { LoginID = _.LoginID, UserName = _.Firstname + “” + _.Lastname + “-” + _.EmployeeID}).ToList(), “LoginID”, “UserName”);
ViewBag.ProjectID = new SelectList(db.ProjectMasters.Where(_ => _.Status.Equals(“1”)).ToList(), “ID”, “ProjectName”);
}
return View();
}

[HttpGet]
public ActionResult EmployeeWorkReportFilter(string EmpLoginID, string ProjectID, string WorkDateFrom, string WorkDateTo)
{
//EmployeeWorkReportFilter
using (DataAccessLayer.DatabaseContext db = new DataAccessLayer.DatabaseContext())
{
int EmpID = 0, Projid = 0;
DateTime _fromDate = DateTime.Now.Date; DateTime _ToDate = DateTime.Now.Date;
bool isFromDateValid = false, istoDateValid = false;
List partialdata = db.EmployeeDailyWorks.ToList();

// Filter For Employee
if (int.TryParse(EmpLoginID, out EmpID))
{
partialdata = partialdata.Where(_ => _.LoginID.Value.Equals(EmpID)).ToList();
}
// Filter for Project
if (int.TryParse(ProjectID, out Projid))
{
partialdata = partialdata.Where(_ => _.ProjectID != null && _.ProjectID.Value.Equals(Projid)).ToList();
}
// Filter For date
if (!string.IsNullOrEmpty(WorkDateFrom))
{
if (DateTime.TryParse(WorkDateFrom, out _fromDate))
{
isFromDateValid = true;
}
else
{
ModelState.AddModelError(string.Empty, “Please enter valid date”);
return View();
}

}
if (!string.IsNullOrEmpty(WorkDateTo))
{
if (DateTime.TryParse(WorkDateTo, out _ToDate))
{
istoDateValid = true;
}
else
{
ModelState.AddModelError(string.Empty, “Please enter valid date”);
return View();
}

}

if (isFromDateValid && istoDateValid)
{
if (_fromDate > _ToDate)
{
ModelState.AddModelError(string.Empty, “Please enter valid date”);
return View();
}
else
{
partialdata = partialdata.Where(_ => _.WorkDate >= _fromDate && _.WorkDate _.WorkDate >= _fromDate).ToList();
}
else if (istoDateValid)
{
partialdata = partialdata.Where(_ => _.WorkDate <= _ToDate).ToList();
}

if (partialdata.Any())
{
ViewBag.employeemaster = db.Employees.ToList();
ViewBag.projectmaster = db.ProjectMasters.ToList();
ViewBag.SchoolMaster = db.SchoolMasters.ToList();
ViewBag.TaskMaster = db.TaskMasters.ToList();
}

return PartialView("EmployeeWorkReportFilter", partialdata);
}

}
#endregion

________________________________View page codng ___________
@model IEnumerable
@{
ViewBag.Title = “EmployeeWorkReport”;
Layout = “~/Views/Shared/_LayoutAdmin.cshtml”;
}
http://~/Scripts/jquery-2.1.4.min.js
http://~/Scripts/jquery.validate.min.js
http://~/Scripts/jquery.validate.unobtrusive.min.js

.

@Html.DropDownList(“LoginID”, ViewBag.LoginID as SelectList, “Select Employee”, new { @class = “form-control” })
@Html.DropDownList(“ProjectID”, ViewBag.ProjectID as SelectList, “Select Project”, new { @class = “form-control” })

@Html.Hidden(“hiddenhref”)
@*@Html.Action(“EmployeeWorkReportFilter”)*@

@System.Web.Optimization.Scripts.Render(“~/Scripts/jquery-1.12.4.js”)
@System.Web.Optimization.Scripts.Render(“~/Scripts/jquery-ui-1.12.0.js”)
@System.Web.Optimization.Styles.Render(“~/Content/themes/base/jquery-ui.css”)

$(‘input[type=datetime]’).datepicker({
inline: true,
dateFormat: “yy-MM-dd”,
changeMonth: true,
changeYear: true,
yearRange: “-60:+0”
});

function callreport() {
var _EmpLoginID = $(“#LoginID”).val();
var _ProjectID = $(“#ProjectID”).val();
var _WorkDateFrom = $(“#WorkDateFrom”).val();
var _WorkDateTo = $(“#WorkDateTo”).val();
var url = “/AdminReport/EmployeeWorkReportFilter”;
$.get(url, { EmpLoginID: _EmpLoginID, ProjectID: _ProjectID, WorkDateFrom: _WorkDateFrom, WorkDateTo: _WorkDateTo }, function (response) {

/*—–Code Start for partial view webgrid pagination to work with main view and on click of pagination grid open in parent view page———*/
var $response = $(response);
$response.html($response.html().replace(‘/AdminReport/EmployeeWorkReportFilter’, ‘/AdminReport/EmployeeWorkReport’));
/*——Code End——–*/
$(“#employeePartalListGrid”).html($response);
});
}

.gridTable {
margin: 5px;
padding: 10px;
border: 1px #c8c8c8 solid;
border-collapse: collapse;
min-width: 550px;
background-color: #fff;
color: #fff;
width: 100%;
}

.gridHead th {
font-weight: bold;
background-color: #030D8D;
color: #fff;
padding: 10px;
text-align: left;
}

.gridHead a:link, .gridHead a:visited, .gridHead a:active, .gridHead a:hover {
color: #fff;
}

.gridHead a:hover {
text-decoration: underline;
}

.gridTable tr.gridAltRow {
background-color: #efeeef;
}

.gridTable tr:hover {
background-color: #f6f70a;
}

.gridAltRow td {
padding: 10px;
margin: 5px;
color: #333;
text-align: left;
}

.gridRow td {
padding: 10px;
color: #333;
text-align: left;
}

.gridFooter td {
padding: 10px;
color: #ffffff;
font-size: 12pt;
text-align: center;
background-color: #228B22;
}

.gridFooter a {
font-weight: bold;
color: blue;
border: 2px #f00 solid;
}

_____________________________Partail view grid coding ____________
@model List

@{
ViewBag.Title = “EmployeeWorkReportFilter”;

}

@{

var grid = new WebGrid(source: Model, canPage: true, canSort: true, rowsPerPage: 10, ajaxUpdateContainerId: “grid”);

@grid.GetHtml(mode: WebGridPagerModes.All,
tableStyle: “gridTable”,
headerStyle: “gridHead”,
footerStyle: “gridFooter”,
rowStyle: “gridRow”,
alternatingRowStyle: “gridAltRow”,
htmlAttributes: new { id = “grid” },
fillEmptyRows: false,
columns: new[]{
grid.Column(“Sr.No.”, format: item => item.WebGrid.Rows.IndexOf(item) + 1),
grid.Column(“Name”, format: item => item.Employee == null ? “” : item.Employee.Firstname + “” + item.Employee.Lastname + “-” + item.Employee.EmployeeID),
grid.Column(“WorkDate”, format: item => @Convert.ToDateTime(item.WorkDate).ToString(“yyyy MMM dd”)),
grid.Column(“ProjectName”, format: item => item.ProjectMaster == null ? “” : item.ProjectMaster.ProjectName),
grid.Column(“SchoolName”, format: item => item.SchoolMaster == null ? “” : item.SchoolMaster.SchoolName),
grid.Column(“TaskName”, format: item => item.TaskMaster == null ? “” : item.TaskMaster.TaskName),
grid.Column(“DailyWorkDetail”, header: “WorkDetail”),
grid.Column(“Status”,format: (item) =>
{
if (item.Status == “1”)
{

return Html.Raw(“Active”);
}
else
{

return Html.Raw(“Inactive”);
}
}
)}

)
}

@*/*—–Code Start for partial view webgrid pagination to work with main view and on click of pagination grid open in parent view page———*/*@
@System.Web.Optimization.Scripts.Render(“~/Scripts/jquery-1.12.4.js”)

$(‘th a’).click(function () {
var form = $(‘#gridposition’);
form.attr(“action”, this.href);
$(this).attr(“href”, “javascript:”);
form.submit();

});

@*/*——Code End——–*/*@

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s