Bind Grid In MVC:
Step 1:
Controllers Page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using eCRM_MastersLib;
using BAL;
using eCRM_Entity;
namespace eCRM.Controllers.eCRM_Masters
{
public class M_DesignationController : Controller
{
//
// GET: /G_Designation/
Designation mobjModel = new Designation();
eCRM_MastersEntities1 entities = new eCRM_MastersEntities1();
[HttpGet]
public ActionResult _Designation()
{
return View();
}
public ActionResult WebGrid(int page = 1, string sort = "name", string sortDir = "ASC")
{
const int pageSize = 5;
var totalRows = mobjModel.CountDesignation();
sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";
var validColumns = new[] { "DesginationId", "DesignationName", "CreatedDate", "IsActive" };
if (!validColumns.Any(c => c.Equals(sort, StringComparison.CurrentCultureIgnoreCase)))
sort = "DesignationId";
var designation = mobjModel.GetDesignationPage(page, pageSize, "it." + sort + " " + sortDir);
var data = new PagedCustomerModel()
{
TotalRows = totalRows,
PageSize = pageSize,
Designation = designation
};
return View(data);
}
public class PagedCustomerModel
{
public int TotalRows { get; set; }
public IEnumerable<tbl_Designation> Designation { get; set; }
public int PageSize { get; set; }
}
public ActionResult Create()
{
if (Request.IsAjaxRequest())
{
ViewBag.IsUpdate = false;
return View("_Designation");
}
else
return View();
}
public ActionResult View(int id)
{
var data = mobjModel.GetDesignation1(id);
if (Request.IsAjaxRequest())
{
MS_DesignationMaster cust = new MS_DesignationMaster();
cust.DesignationName = data.DesignationName;
//cust.Name = data.Name;
//cust.Address = data.Address;
//cust.ContactNo = data.ContactNo;
return View("_ViewDesignation", cust);
}
else
return View(data);
}
public ActionResult Edit(int id)
{
var data = mobjModel.GetDesignation1(id);
if (Request.IsAjaxRequest())
{
MS_DesignationMaster cust = new MS_DesignationMaster();
cust.DesignationName = data.DesignationName;
cust.DesginationId = data.DesginationId;
//cust.Name = data.Name;
//cust.Address = data.Address;
//cust.ContactNo = data.ContactNo;
ViewBag.IsUpdate = true;
return View("_Designation", cust);
}
else
return View(data);
}
public ActionResult Delete(int id)
{
tbl_Designation mobjcust = new tbl_Designation();
mobjcust.DesginationId = id;// mCust.DesginationId;
//mobjcust.Address = mCust.Address;
//mobjcust.ContactNo = mCust.ContactNo;
//mobjcust.Name = mCust.Name;
bool check = mobjModel.DeleteDesignation(mobjcust);
if (check)
{
TempData["Msg"] = "Data has been Deleted succeessfully";
ModelState.Clear();
return RedirectToAction("WebGrid", "M_Designation");
}
else
{
return PartialView("_Designation");
}
//bool check = mobjModel.DeleteDesignation(id);
//var data = mobjModel.GetDesignation();
//return RedirectToAction("WebGrid");
}
[HttpPost]
public ActionResult CreateEditDesignaion(MS_DesignationMaster mCust, string Command)
{
// Validate the model being submitted
if (!ModelState.IsValid)
{
return PartialView("_Designation", mCust);
}
else if (Command == "Save")
{
tbl_Designation mobjcust = new tbl_Designation();
tbl_Designation isexist = entities.tbl_Designation.Where(x => x.DesignationName == mCust.DesignationName).FirstOrDefault();
if (isexist != null)
{
TempData["Msg"] = "Designation is allready exists";
ModelState.Clear();
return RedirectToAction("WebGrid", "M_Designation");
}
else
{
mobjcust.DesignationName = mCust.DesignationName;
mobjcust.IsActive = true;
//mobjcust.Address = mCust.Address;
//mobjcust.ContactNo = mCust.ContactNo;
//mobjcust.Name = mCust.Name;
bool check = mobjModel.CreateDesignation(mobjcust);
if (check)
{
TempData["Msg"] = "Data has been saved succeessfully";
ModelState.Clear();
return RedirectToAction("WebGrid", "M_Designation");
}
}
}
else if (Command == "Update")
{
tbl_Designation mobjcust = new tbl_Designation();
mobjcust.DesignationName = mCust.DesignationName;
mobjcust.DesginationId = mCust.DesginationId;
//mobjcust.Address = mCust.Address;
//mobjcust.ContactNo = mCust.ContactNo;
//mobjcust.Name = mCust.Name;
bool check = mobjModel.UpdateDesignation(mobjcust);
if (check)
{
TempData["Msg"] = "Data has been updated succeessfully";
ModelState.Clear();
return RedirectToAction("WebGrid", "M_Designation");
}
}
return PartialView("_Designation");
}
}
}
Step2:
Model Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using System.Linq.Expressions;
using eCRM_Entity;
using BAL;
namespace eCRM_MastersLib
{
public class Designation : IDisposable
{
bool status;
private readonly eCRM_MastersEntities1 entities = new eCRM_MastersEntities1();
public IEnumerable<tbl_Designation> GetDesignation()
{
return entities.tbl_Designation.Where(m => m.IsActive==true).ToList();
}
public IEnumerable<tbl_Designation> GetDesignationPage(int pageNumber, int pageSize, string searchCriteria)
{
if (pageNumber < 1)
pageNumber = 1;
return entities.tbl_Designation
// .OrderBy(searchCriteria)
//.Skip((pageNumber - 1) * pageSize)
.Take(pageSize)
.ToList();
}
public class PagedCustomerModel
{
public int TotalRows { get; set; }
public IEnumerable<tbl_Designation> Designation { get; set; }
public int PageSize { get; set; }
}
public int CountDesignation()
{
return entities.tbl_Designation.Count();
}
public void Dispose()
{
entities.Dispose();
}
public tbl_Designation GetDesignation1(int mDesgID)
{
return entities.tbl_Designation.Where(m => m.DesginationId == mDesgID).FirstOrDefault();
}
public bool CreateDesignation(tbl_Designation des)
{
try
{
using (var db = new eCRM_MastersEntities1())
{
var dc = db.tbl_Designation.Create();
dc.DesignationName = des.DesignationName;
// dc.DesginationId = Designationdetails.DesginationId;
dc.CreatedBy = 1;//int.Parse(des.CreatedBy.ToString());
dc.CreatedDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
db.tbl_Designation.Add(dc);
db.SaveChanges();
//status = true;
// return RedirectToAction("index", "index");
}
return true;
}
catch (Exception mex)
{
return false;
}
}
public bool UpdateDesignation(tbl_Designation mDesignation)
{
try
{
tbl_Designation data = entities.tbl_Designation.Where(m => m.DesginationId == mDesignation.DesginationId).FirstOrDefault();
data.DesignationName = mDesignation.DesignationName;
//data.Address = mCustomer.Address;
//data.ContactNo = mCustomer.ContactNo;
entities.SaveChanges();
return true;
}
catch (Exception mex)
{
return false;
}
}
public bool DeleteDesignation(tbl_Designation mDesignation)
{
try
{
tbl_Designation data = entities.tbl_Designation.Where(m => m.DesginationId == mDesignation.DesginationId).FirstOrDefault();
data.IsActive = false;
//entities.tbl_Designation.DeleteObject(data);
entities.SaveChanges();
return true;
}
catch (Exception mex)
{
return false;
}
}
public bool CreateEditDesignation(tbl_Designation des, string Command)
{
// Validate the model being submitted
//if (!ModelState.IsValid)
//{
// return PartialView("_Designation", mCust);
//}
if (Command == "Save")
{
using (var db = new eCRM_MastersEntities1())
{
var dc = db.tbl_Designation.Create();
dc.DesignationName = des.DesignationName;
dc.IsActive = des.IsActive;
// dc.DesginationId = Designationdetails.DesginationId;
dc.CreatedBy = 1;//int.Parse(des.CreatedBy.ToString());
dc.CreatedDate = Convert.ToDateTime(System.DateTime.Now.ToShortDateString());
db.tbl_Designation.Add(dc);
db.SaveChanges();
status = true;
// return RedirectToAction("index", "index");
}
}
else if (Command == "Update")
{
tbl_Designation mobjcust = new tbl_Designation();
Designation ab = new Designation();
// mobjcust.CustID = mCust.CustID;
// mobjcust.Address = mCust.Address;
// mobjcust.ContactNo = mCust.ContactNo;
mobjcust.DesignationName = des.DesignationName;
bool check = ab.UpdateDesignation(mobjcust);
if (check)
{
//TempData["Msg"] = "Data has been updated succeessfully";
//ModelState.Clear();
// return RedirectToAction("WebGrid", "G_Designation");
status = true;
}
else
{
status = false;
}
//}
//return PartialView("_Customer");
}
return status;
}
void IDisposable.Dispose()
{
throw new NotImplementedException();
}
}
}
Step3:
View Code:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
ViewBag.Title = "Designation Master";
}
@{
eCRM_MastersLib.Designation b = new eCRM_MastersLib.Designation();
WebGrid grid = new WebGrid();
grid.Bind(b.GetDesignation() );
}
<link href="../../Content/themes/base/minified/jquery-ui.min.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.20.min.js" type="text/javascript"></script>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
$(document).ready(function () {
$("#CreateCustomer").live("click", function (e) {
// e.preventDefault(); use this or return false
var url = $(this).attr('href');
$("#dialog-edit").dialog({
title: 'Create TaskStatus',
autoOpen: false,
resizable: false,
height: 355,
width: 400,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
open: function (event, ui) {
$(this).load(url);
},
close: function (event, ui) {
$(this).dialog('close');
}
});
$("#dialog-edit").dialog('open');
return false;
});
$(".editDialog").live("click", function (e) {
// e.preventDefault(); use this or return false
var url = $(this).attr('href');
$("#dialog-edit").dialog({
title: 'Edit TaskStatus',
autoOpen: false,
resizable: false,
height: 355,
width: 400,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
open: function (event, ui) {
$(this).load(url);
},
close: function (event, ui) {
$(this).dialog('close');
}
});
$("#dialog-edit").dialog('open');
return false;
});
$(".confirmDialog").live("click", function (e) {
// e.preventDefault(); use this or return false
var url = $(this).attr('href');
$("#dialog-confirm").dialog({
autoOpen: false,
resizable: false,
height: 170,
width: 350,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
buttons: {
"OK": function () {
$(this).dialog("close");
window.location = url;
},
"Cancel": function () {
$(this).dialog("close");
}
}
});
$("#dialog-confirm").dialog('open');
return false;
});
$(".viewDialog").live("click", function (e) {
// e.preventDefault(); use this or return false
var url = $(this).attr('href');
$("#dialog-view").dialog({
title: 'View TaskStatus',
autoOpen: false,
resizable: false,
height: 250,
width: 400,
show: { effect: 'drop', direction: "up" },
modal: true,
draggable: true,
open: function (event, ui) {
$(this).load(url);
},
buttons: {
"Close": function () {
$(this).dialog("close");
}
},
close: function (event, ui) {
$(this).dialog('close');
}
});
$("#dialog-view").dialog('open');
return false;
});
$("#btncancel").live("click", function (e) {
// location.reload(true);
$("#dialog-edit").dialog('close');
});
});
</script>
<div style="height:10px;background-color:Yellow;">
</div>
<h2>
Designation Master</h2>
<br />
<div style="color: Green; font-weight: bold">
@TempData["msg"]
</div>
<br />
@grid.GetHtml(
fillEmptyRows: false,
tableStyle: "webgrid",
headerStyle: "webgrid-header",
footerStyle: "webgrid-footer",
alternatingRowStyle: "webgrid-alternating-row",
selectedRowStyle: "webgrid-selected-row",
rowStyle: "webgrid-row-style", htmlAttributes: new { id = "grid" },
mode: WebGridPagerModes.All,
firstText: "<< First",
previousText: "< Prev",
nextText: "Next >",
lastText: "Last >>",
columns: new[] {
grid.Column("DesginationId",
header: "DesginationId", canSort: false),
grid.Column("DesignationName",
header: "DesignationName",
format: @<text>
@Html.ActionLink((string)item.DesignationName, "View", new { id = item.DesginationId }, new { @class = "viewDialog" })</text>
),
@*grid.Column("Address"),*@
@* grid.Column("ContactNo",
header: "Contact No"
),*@
grid.Column("",
header: "Actions",
format: @<text>
@Html.ActionLink("Edit", "Edit", new { id = item.DesginationId }, new { @class = "editDialog"/*, data_dialog_id = "edit-Dialog"*/ })
|
@Html.ActionLink("Delete", "Delete", new { id = item.DesginationId }, new { @class = "confirmDialog" })
</text>
)
})
<br />
<a id="CreateCustomer" href="M_Designation\Create" class="Create">Create Designation</a>
<div id="dialog-confirm" style="display: none">
<p>
<span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>
Are you sure to delete ?
</p>
</div>
<div id="dialog-edit" style="display: none">
</div>
<div id="dialog-view" style="display: none">
</div>
Enj...Hope it will help You..
No comments:
Post a Comment