Ajax Pagination with ASP.Net MVC

// GET: TransactionsCA
public ActionResult Index()
{
string strpost = “&ajax=1”;
int starting = 0;
if (Request.Form[“starting”] != null)
{
starting = Convert.ToInt32(Request.Form[“starting”]);
}
var q_total = (from t in db.MyTransactionCA select t);
int numrows = q_total.Count();

//var query = (from c in db.Projects orderby c.ProjectId descending select c).AsEnumerable().Skip(starting).Take(2);
var TVM = (from t in db.MyTransactionCA
join a in db.MyAgency on t.Agency_Id equals a.AgencyId
select new TransVM
{
Agency_Name = a.AgencyName,
TransactionCAId = t.TransactionCAId,
PBusinessSolution = t.PBusinessSolution,
MonetaryCost = t.MonetaryCost,
MonetoryBenefit = t.MonetoryBenefit,
LaborCost = t.LaborCost,
LaborBenefite =t.LaborBenefite,
OtherCost = t.OtherCost,
OtherBenefite = t.OtherBenefite,
TotalCost = t.TotalCost,
TotalBenefit =t.TotalBenefit,
PriorityRatio =t.PriorityRatio,

}
).AsEnumerable().Skip(starting).Take(2);
string links = MJPageLib.paginate(numrows, starting, 2, “”, “page”, strpost);
ViewBag.link = links;
ViewBag.query = TVM;
if (Request.IsAjaxRequest())
return PartialView(“Index2”);

return View();
}

 

// GET: TransactionCAs/Create
public ActionResult Create()
{
var TVM = new TransVM();
var AllAgency = db.MyAgency.ToList();
TVM.Agencies = AllAgency;
return View(“Create”,TVM);
}

// POST: TransactionCAs/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = “TransactionCAId,PBusinessSolution,MonetaryCost,MonetoryBenefit,LaborCost,LaborBenefite,OtherCost,OtherBenefite,Agency_ID”)] TransVM TransVM)
{
if (ModelState.IsValid)
{

var total_cost = TransVM.MonetaryCost + TransVM.LaborCost;
var total_benefit = TransVM.MonetoryBenefit + TransVM.LaborBenefite;
double total_ratio = 0;
if(total_cost>0)
{
total_ratio = total_benefit/total_cost;
}
else
{
var AllAgency = db.MyAgency.ToList();
TransVM.Agencies = AllAgency;
return View(“Create”,TransVM);
}

//db.MyTransactionCA.Add(transactionCA);
var TransObj = new TransactionCA
{

PBusinessSolution = TransVM.PBusinessSolution,
MonetaryCost = TransVM.MonetaryCost,
MonetoryBenefit =TransVM.MonetoryBenefit,
LaborCost=TransVM.LaborCost,
LaborBenefite =TransVM.LaborBenefite,
OtherCost = TransVM.OtherCost,
OtherBenefite =TransVM.OtherBenefite,
DateCreated = DateTime.Now,
Agency_Id = TransVM.Agency_ID,
TotalCost = total_cost,
TotalBenefit = total_benefit,
PriorityRatio = total_ratio,

};
db.MyTransactionCA.Add(TransObj);
db.SaveChanges();
return RedirectToAction(“Index”);
}

return View(TransVM);
}

// GET: TransactionCAs/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
TransactionCA transactionCA = db.MyTransactionCA.Find(id);
if (transactionCA == null)
{
return HttpNotFound();
}
PopulateAgencyDropDownList(transactionCA.Agency_Id);
return View(transactionCA);
}

// POST: TransactionCAs/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = “TransactionCAId,PBusinessSolution,MonetaryCost,MonetoryBenefit,LaborCost,LaborBenefite,OtherCost,OtherBenefite,TotalCost,TotalBenefit,PriorityRatio,Agency_Id,DateCreated,UserCreated,DateModified,UserModified”)] TransactionCA transactionCA)
{
if (ModelState.IsValid)
{
db.Entry(transactionCA).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction(“Index”);
}
return View(transactionCA);
}

private void PopulateAgencyDropDownList(object selectedAgency = null)
{
var agencyquery = from d in db.MyAgency
orderby d.AgencyName
select d;
ViewBag.Agency_Id = new SelectList(agencyquery, “AgencyId”, “AgencyName”, selectedAgency);
}

———————————-

View:

Index:

@model IEnumerable<CAuth.ViewModels.TransVM>
<div id=”page”>
<div>
<p style=”float:left”>@Html.ActionLink(“Create New”, “Create”)</p>
<center><h3>Cost Benefite Analysis</h3></center>

</div>
<table class=”table”>
<tr>
<th>
Agency
</th>
<th>
@Html.DisplayNameFor(model => model.PBusinessSolution)
</th>
<th>
@Html.DisplayNameFor(model => model.MonetaryCost)
</th>
<th>
@Html.DisplayNameFor(model => model.MonetoryBenefit)
</th>
<th>
@Html.DisplayNameFor(model => model.LaborCost)
</th>
<th>
@Html.DisplayNameFor(model => model.LaborBenefite)
</th>
<th>
@Html.DisplayNameFor(model => model.OtherCost)
</th>
<th>
@Html.DisplayNameFor(model => model.OtherBenefite)
</th>
<th>@Html.DisplayNameFor(model => model.TotalCost)</th>
<th>@Html.DisplayNameFor(model => model.TotalBenefit)</th>
<th>@Html.DisplayNameFor(model => model.PriorityRatio)</th>
<th>Action</th>
</tr>

@foreach (var item in ViewBag.query)
{
<tr>
<td>@item.Agency_Name</td>
<td>@item.PBusinessSolution</td>
<td>@item.MonetaryCost</td>
<td>@item.MonetoryBenefit</td>
<td>@item.LaborCost</td>
<td>@item.LaborBenefite</td>
<td>@item.OtherCost</td>
<td>@item.OtherBenefite</td>
<td>@item.TotalCost</td>
<td>@item.TotalBenefit</td>
<td>@item.PriorityRatio</td>
<td>
@Html.ActionLink(“Edit”, “Edit”, new { id = item.TransactionCAId })
</td>
</tr>
}
<tr>
<td colspan=”12″>
@Html.Raw(ViewBag.link)
</td>
</tr>
</table>
</div>

——————————————————

View Create:

@model CAuth.ViewModels.TransVM

@{
ViewBag.Title = “Create”;
}
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class=”form-horizontal”>
<h4>Create New Transaction Cost Analysis</h4>
<hr />
@Html.ValidationSummary(true, “”, new { @class = “text-danger” })
<div class=”form-group”>
@Html.LabelFor(model => model.Agency, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.DropDownListFor(m => m.Agency_ID, new SelectList(Model.Agencies, “AgencyId”, “AgencyName”), “–Please Select Agency–“, new {@class=”form-control”,@required=”required” })
@Html.ValidationMessageFor(model => model.Agency, “”, new { @class = “text-danger” })
</div>
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.PBusinessSolution, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.PBusinessSolution, new { htmlAttributes = new { @class = “form-control”,@required=”required” } })
@Html.ValidationMessageFor(model => model.PBusinessSolution, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.MonetaryCost, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10″>
<input type=”number” name=”MonetaryCost” id=”MonetaryCost” required class=”form-control” />
@Html.ValidationMessageFor(model => model.MonetaryCost, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.MonetoryBenefit, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10″>
<input type=”number” class=”form-control” required name=”MonetoryBenefit” id=”MonetoryBenefit” />
@Html.ValidationMessageFor(model => model.MonetoryBenefit, “”, new { @class = “text-danger” })</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.LaborCost, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10″>
<input type=”number” name=”LaborCost” id=”LaborCost” required class=”form-control” />
@Html.ValidationMessageFor(model => model.LaborCost, “”, new { @class = “text-danger” })</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.LaborBenefite, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10″>
<input type=”number” name=”LaborBenefite” id=”LaborBenefite” class=”form-control” required />
@Html.ValidationMessageFor(model => model.LaborBenefite, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.OtherCost, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.OtherCost, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.OtherCost, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.OtherBenefite, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.OtherBenefite, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.OtherBenefite, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group” style=”margin-right:67%; margin-top:3%; margin-bottom:3%;”>
<div class=”col-md-offset-2 col-md-10″>
<input type=”submit” value=”Create” class=”btn btn-default” />
</div>
</div>
</div>
}

<div style=” margin-right:62%;”>
@Html.ActionLink(“Back to List”, “Index”)
</div>

 

————————————————————-

Edit View:

@model CAuth.Models.TransactionCA

@{
ViewBag.Title = “Edit”;
}

<h2>Edit</h2>

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class=”form-horizontal”>
<h4>TransactionCA</h4>
<hr />
@Html.ValidationSummary(true, “”, new { @class = “text-danger” })
@Html.HiddenFor(model => model.TransactionCAId)

<div class=”form-group”>
<label class=”control-label col-md-2″ for=”DepartmentID”>Agency</label>
<div class=”col-md-10″>
@Html.DropDownList(“Agency_Id”, null, htmlAttributes: new { @class = “form-control” })
@Html.ValidationMessageFor(model => model.Agency_Id, “”, new { @class = “text-danger” })
</div>
</div>
<div class=”form-group”>
@Html.LabelFor(model => model.PBusinessSolution, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.PBusinessSolution, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.PBusinessSolution, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.MonetaryCost, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.MonetaryCost, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.MonetaryCost, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.MonetoryBenefit, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.MonetoryBenefit, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.MonetoryBenefit, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.LaborCost, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.LaborCost, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.LaborCost, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.LaborBenefite, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.LaborBenefite, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.LaborBenefite, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.OtherCost, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.OtherCost, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.OtherCost, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group”>
@Html.LabelFor(model => model.OtherBenefite, htmlAttributes: new { @class = “control-label col-md-2″ })
<div class=”col-md-10”>
@Html.EditorFor(model => model.OtherBenefite, new { htmlAttributes = new { @class = “form-control” } })
@Html.ValidationMessageFor(model => model.OtherBenefite, “”, new { @class = “text-danger” })
</div>
</div>

<div class=”form-group” style=”margin-right:67%; margin-top:3%; margin-bottom:3%;”>
<div class=”col-md-offset-2 col-md-10″>
<input type=”submit” value=”Create” class=”btn btn-default” />
</div>
</div>
</div>
}

<div style=” margin-right:62%;”>
@Html.ActionLink(“Back to List”, “Index”)
</div>

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.