How to display the difference between 2 columns in a table - c#

I am having an Audit Table that displays 2 columns OLD Data, New data which are in XML while displaying the table, I want only the differences between the 2 columns to be listed.
I have converted the data to XML to present my old and new data and I have removed the XML initialization for easy readability.
Controller
public async Task < IActionResult > History(string RefNum) {
if (RefNum == null) {
return RedirectToAction("Index");
}
var obj = await (from x in _db.AuditModel select x).Where(x => x.RefNum.Equals(RefNum)).ToListAsync();
if (obj == null) {
return RedirectToAction("Index");
}
return PartialView(obj);
}
View Page
#model IEnumerable<Flight_info.Models.AuditModel>
<div>
<div class="modal-header">
<h2 class="modal-title">
HISTORY
</h2>
<button class="close" data-dismiss="modal" align="right">×</button>
</div>
<div class="modal-body">
<table class="table" style="table-layout: fixed; word-wrap: break-word">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.RefNum)
</th>
<th>
#Html.DisplayNameFor(model => model.ChangeMade)
</th>
<th>
#Html.DisplayNameFor(model => model.OldData)
</th>
<th>
#Html.DisplayNameFor(model => model.NewData)
</th>
<th>
#Html.DisplayNameFor(model => model.ModifiedOn)
</th>
<th>
#Html.DisplayNameFor(model => model.ModifiedBY)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.RefNum)
</td>
<td>
#Html.DisplayFor(modelItem => item.ChangeMade)
</td>
<td>
#Html.DisplayFor(modelItem => item.OldData)
</td>
<td>
#Html.DisplayFor(modelItem => item.NewData)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModifiedOn)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModifiedBY)
</td>
</tr>
}
</tbody>
</table>
<div>
<a asp-action="Index">Back to List </a>
</div>
</div>
</div>
[This is currently my output][1]
[1]: https://i.stack.imgur.com/KeNHk.png
But I want only the differences to be displayed. Please Help.

1- If you want to list only the rows that OldData != NewData, then use filter in Where clause. such as below:
var obj = await (from x in _db.AuditModel select x)
.Where(x => x.RefNum.Equals(RefNum) && x.OldData != x.NewData).ToListAsync();
2-If you want to show all rows and display difference value of OldData and NewData then write below code in your view
<td>
#Html.DisplayFor(modelItem => item.OldData - item.NewData)
</td>

i found the solution myself.Instead of using XML i converted my objects to json and i used JSON DIFF.
While displaying i used jquery to dynamically create a table to display the key and value pairs

Related

How to build two <table> tags using One loop inside index View

I am building two conference lists, the right side div (8 col) suppose to contain all conferences, while the left side div (4 col) suppose to contain the featured conferences only. I have performed it using two loops and it will affect the performance.
My question is how to loop through both divs using one foreach loop ?
<table class="table-hover table table-striped table-bordered dt-responsive">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.EventNameAr)
</th>
<th>
#Html.DisplayNameFor(model => model.EventNameEn)
</th>
<th>
#Html.DisplayNameFor(model => model.ApplicationUserId)
</th>
<th>
#Html.DisplayNameFor(model => model.EventType)
</th>
<th>
#Html.DisplayNameFor(model => model.Organizers)
</th>
<th>
#Html.DisplayNameFor(model => model.Country)
</th>
<th>
#Html.DisplayNameFor(model => model.Speciality)
</th>
<th>
#Html.DisplayNameFor(model => model.EventDate)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
<a asp-action="Details" asp-route-id="#item.Id"> #Html.DisplayFor(modelItem => item.EventNameAr)</a>
</td>
<td>
#Html.DisplayFor(modelItem => item.EventNameEn)
</td>
<td>
<a asp-action="Details" asp-controller="ApplicationUsers" asp-route-id="#item.ApplicationUser.Id">
#Html.DisplayFor(model => item.ApplicationUser.ArName)
</a>
<img class="img-rounded" src="#Url.Content("~/" + #ARID.Properties.Resources.ProfileImageFolder + "/" + item.ApplicationUser.ProfileImage)" width="50" height="50" /><br />
</td>
<td>
#Html.DisplayFor(modelItem => item.EventType)
</td>
<td>
#Html.Raw(item.Organizers)
</td>
<td>
#Html.DisplayFor(modelItem => item.Country.ArCountryName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Speciality.ArSpecialityName)
</td>
<td>
#Html.DisplayFor(modelItem => item.EventDate)
</td>
<td>
#if (User.IsInRole("Admins") || item.ApplicationUserId == currentUser.Id)
{
<a asp-action="Edit" asp-route-id="#item.Id"><span class="glyphicon glyphicon-edit"></span></a>
}
#if (User.IsInRole("Admins"))
{
<a asp-action="Delete" asp-route-id="#item.Id"><span class="glyphicon glyphicon-trash"></span></a>
}
</td>
</tr>
}
</tbody>

PagedList<T> does not contain a definition for the properties of my Model <T>

I have this partial View as below:
#model IPagedList<Student>
<h2>List of Students</h2>
<div id="studentList">
<div class="pagedList" data-uwa-target="#studentList">
#Html.PagedListPager(Model,page=>Url.Action("Index",new { page }),
PagedListRenderOptions.MinimalWithItemCountText)
</div>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.LastName) //here error
</th>
<th>
#Html.DisplayNameFor(model => model.FirstName) //here error
</th>
<th>
#Html.DisplayNameFor(model => model.City) //here error
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.City)
</td>
</tr>
}
</table>
<div class="row">
<div class="col-sm-6" id="slid">
<button id="export" class="btn btn-default btn-primary" onclick="location.href='#Url.Action("ExportInCSVFormat")';return false;">Export Student List in CSV Format</button>
</div>
<div class="col-sm-4" id="excelSlide">
<button id="excelExport" class="btn btn-default btn-success" onclick="location.href='#Url.Action("ExportStudentsInExel")';return false;">Export Student List in Excel Format</button>
</div>
</div>
My Index.cshmtl is :
#model IPagedList<Student>
#{
ViewBag.Title = "Index";
}
#Html.Partial("_Student", Model)
My StudentController is
public ActionResult Index(int page=1)
{
var model = dbAllStudents.Students
.OrderByDescending(p => p.LastName)
.ToPagedList(page, 10);
return View(model);
}
The problem happens to "#Html.DisplayNameFor(model => model.LastName)". It is saying that " IPaged does not contatin a definition for LastName.
I have an idea that the problem is caused because to the Index method of Student Controller I need to add .Select but I am not sure for that. I would really appreciate if someone can help me
It's not working, because overloaded method which you want to use doesn't fit with your interface. It's for IEnumerable<> types. You can access to the first element in the list and it should returns you correct value even if list is empty.
Change your table definition to:
<table class="table">
<tr>
<th>
#Html.DisplayNameFor( => Model[0].LastName)
</th>
<th>
#Html.DisplayNameFor(_ => Model[0].FirstName)
</th>
<th>
#Html.DisplayNameFor(_ => Model[0].City)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(_ => item.LastName)
</td>
<td>
#Html.DisplayFor(_ => item.FirstName)
</td>
<td>
#Html.DisplayFor(_ => item.City)
</td>
</tr>
}
</table>
and everything will be ok.

ASP.NET MVC view not displaying correct info from database

So I am building a report to be displayed on our website, I created a view for the qry and dropping it into our .EDMX file. I can set up the view for the site with that query however the data in the database is not matching up with what is being shown on screen. The dates are what is messing up, everything else matches. Any thoughts as to what is wrong?
SELECT TOP 1000 [Name]
,[Manufacturer]
,[SerialNumber]
,[TSIFBarcode]
,[NextCalibrationDate]
,[Description]
,[CalibrationCost]
,[Location]
,[RoutineInspection]
,[Comment]
,[Priority]
FROM [InsertDBHere]
That is my sql code, below is my code to create the view from my controller:
IEnumerable<qryCalibrationEquipmentTracker> view = db.qryCalibrationEquipmentTrackers;
view = view.OrderBy(a => a.NextCalibrationDate);
return View(view);
Below is code for the view
#model IEnumerable<MvcDBFirst.Models.qryCalibrationEquipmentTracker>
#{
ViewBag.Title = "Calibration Tracking Report";
}
<h2>Calibration Tracking Report</h2>
#if (ViewBag.PrintMode == null || !ViewBag.PrintMode)
{
<div class="float-right clear-fix">
<div class="amMenu">
<ul class="amMenuLinksHoriz clear-fix">
<li>
<a href="#Url.Action("ExportCalTracking", "Reports")">
<img src="#Url.Content("~/images/assetmgr/printer.png")" height="25px" alt="Printer" />
</a>
</li>
<li>
#*<a href="#Url.Action("ExcelCalTracking", "Reports")">*#
<a onclick="postTableData('amCalIndexTable')">
<img src="#Url.Content("~/images/assetmgr/excel.png")" height="25px" alt="Excel" />
</a>
</li>
</ul>
</div>
</div>
}
<h2>Report Run on #DateTime.Now.ToString()</h2>
<table class="amCalIndexTable">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Manufacturer)
</th>
<th>
#Html.DisplayNameFor(model => model.SerialNumber)
</th>
<th>
#Html.DisplayNameFor(model => model.TSIFBarcode)
</th>
<th>
#Html.DisplayNameFor(model => model.NextCalibrationDate)
</th>
<th>
#Html.DisplayNameFor(model => model.Priority)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.CalibrationCost)
</th>
<th>
#Html.DisplayNameFor(model => model.Location)
</th>
<th>
#Html.DisplayNameFor(model => model.RoutineInspection)
</th>
<th>
#Html.DisplayNameFor(model => model.Comment)
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Manufacturer)
</td>
<td>
#Html.DisplayFor(modelItem => item.SerialNumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.TSIFBarcode)
</td>
<td>
#Html.DisplayFor(modelItem => item.NextCalibrationDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Priority)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
#*Html.ActionLink(item.Description, "Details", "tblItems",
new
{
type = item.fkItemType,
serialNumber = item.SerialNumber,
owner = item.fkOwner
}, null
)*#
</td>
<td>
#Html.DisplayFor(modelItem => item.CalibrationCost)
</td>
<td>
#Html.DisplayFor(modelItem => item.Location)
</td>
<td>
#Html.DisplayFor(modelItem => item.RoutineInspection)
</td>
<td>
#Html.DisplayFor(modelItem => item.Comment)
</td>
</tr>
}
</tbody>
Convert your view to List before the OrderBy like this. view.ToList().OrderBy().
Like this.
IEnumerable<qryCalibrationEquipmentTracker> view = db.qryCalibrationEquipmentTrackers;
view = view.ToList().OrderBy(a => a.NextCalibrationDate);
return View(view);
This was a Microsoft issue, since it was a query it didnt have a primary key. Microsoft came in and randomly chose a primary key. We have updated our query to deal with this.

Passing IEnumerable EditorFor value through ActionLink to the Action

So, I am new to MVC, and I have a partial view with IEnumerable model, in which I have few fields I would like to be editable.
#model IEnumerable<Allocations>
<p>
#Html.ActionLink("Create New", "AddAllocation", "Admin")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Description)
</th>
<th>
#Html.DisplayNameFor(model => model.Percentage)
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Description)
</td>
<td>
#Html.EditorFor(modelItem => item.Percentage)
</td>
<td>#Html.HiddenFor(modelItem => item.ID)</td>
<td>
#Html.ActionLink("Save", "EditAllocation", "Admin", null, new { id= item.ID })
</td>
</tr>
}
</table>
I want to be able to pass the entered/changed textbox values to the controller action. I understand, I can do that using JQuery if it wasn't a IEnumerable model view. I might be missing something here, and would appreciate if someone could direct me in the right direction.

Display List of MemberOrg's which have Paid yearly Dues?

In my MVC5 Application, I am currently dealing with 2 tables:
[MemberDues] -- [Id], [Year], [Display], [Amount]
[MemberDuesReceived] -- [Id], [MemberDuesId], [MemberOrgId], [PaidByUserId], [TransactionId], [Paid], [DatePaid], [PaymentMethod], [PayPalPaymentId], [PayPalPayerId], [PayPalPaymentState]
I have a View (below) where I display the records for Different [MemberDues]:
#model IEnumerable<PROJECTdev.Models.MemberDues>
<script type="text/javascript" src="~/Areas/Admin/Scripts/Dues/Index.min.js"></script>
<div id="alert" style="display: none;"></div>
<table class="table table-striped">
<thead>
<tr>
<th class="col-md-2">
</th>
<th>
#Html.DisplayNameFor(model => model.Year)
</th>
<th>
#Html.DisplayNameFor(model => model.Display)
</th>
<th>
#Html.DisplayNameFor(model => model.Amount)
</th>
<th>View</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
<span class="glyphicon glyphicon-pencil"></span> Edit
<span class="glyphicon glyphicon-floppy-remove"></span> Delete
</td>
<td>
#Html.DisplayFor(modelItem => item.Year)
</td>
<td>
#Html.EditorFor(modelItem => item.Display, new { htmlAttributes = new { #class = "form-control pull-left", style = "height: 20px; width: 20px;", onclick = "disableEnableDues(" + item.Id + ", this)" } })
</td>
<td>
#Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
<span class="glyphicon glyphicon-list-alt"></span> Paid
</td>
</tr>
}
</tbody>
</table>
What I am attempting to do is have, when the [Paid] button (link) is clicked, a View is brought up which lists all the Member Organizations which have paid their dues. To do this, I have to query [MemberDuesReceived] for the appropriate [MemberDuesId] and return all records which have the matching [MemberDuesId] & [Paid] == true. This I have attempted to below:
public async Task<ActionResult> DuesPaidMembers(int id)
{
MemberDues memberDues = await db.MemberDues.FindAsync(id);
return View(db.MemberDuesReceived.Where(mdr => mdr.MemberDuesId == memberDues.Id && mdr.Paid).ToListAsync());
}
I then created a view called DuesPaidMembers.cshtml which is below:
#model IEnumerable<PROJECTdev.Models.MemberDuesReceived>
#{
ViewBag.Title = "DuesPaidMembers";
Layout = "~/Areas/Admin/Views/Shared/_LayoutAdmin.cshtml";
}
<h2>DuesPaidMembers</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.MemberDues.Id)
</th>
<th>
#Html.DisplayNameFor(model => model.MemberOrganization.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.PaidByUser.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.TransactionId)
</th>
<th>
#Html.DisplayNameFor(model => model.Paid)
</th>
<th>
#Html.DisplayNameFor(model => model.DatePaid)
</th>
<th>
#Html.DisplayNameFor(model => model.PaymentMethod)
</th>
<th>
#Html.DisplayNameFor(model => model.PayPalPaymentId)
</th>
<th>
#Html.DisplayNameFor(model => model.PayPalPayerId)
</th>
<th>
#Html.DisplayNameFor(model => model.PayPalPaymentState)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.MemberDues.Id)
</td>
<td>
#Html.DisplayFor(modelItem => item.MemberOrganization.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.PaidByUser.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.TransactionId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Paid)
</td>
<td>
#Html.DisplayFor(modelItem => item.DatePaid)
</td>
<td>
#Html.DisplayFor(modelItem => item.PaymentMethod)
</td>
<td>
#Html.DisplayFor(modelItem => item.PayPalPaymentId)
</td>
<td>
#Html.DisplayFor(modelItem => item.PayPalPayerId)
</td>
<td>
#Html.DisplayFor(modelItem => item.PayPalPaymentState)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
#Html.ActionLink("Details", "Details", new { id=item.Id }) |
#Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
Currently however, when I click the button, my appropriate Controller Action (DuesPaidMember()) is called, but when the line return View(db.MemberDuesReceived.Where(mdr => mdr.MemberDuesId == memberDues.Id && mdr.Paid).ToListAsync()); is hit, I receive the following error:
The model item passed into the dictionary is of type 'System.Threading.Tasks.Task1[System.Collections.Generic.List1[PRISMdev.Models.MemberDuesReceived]]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[PRISMdev.Models.MemberDuesReceived]'.`
Does anyone have any thoughts on what I'm apparently doing wrong?
The following line is to blame.
return View(db.MemberDuesReceived
.Where(mdr => mdr.MemberDuesId == memberDues.Id && mdr.Paid)
.ToListAsync());
You are passing the result of ToListAsync as your model, which is a Task<List<PRISMdev.Models.MemberDuesReceived>>.
Instead, insert await so that the task is run and the resulting value of type List<PRISMdev.Models.MemberDuesReceived> is returned as your model:
return View(await db.MemberDuesReceived
.Where(mdr => mdr.MemberDuesId == memberDues.Id && mdr.Paid)
.ToListAsync());

Categories

Resources