How to create a table from a distinct in mvc? - c#

I have a model with an element vchClient and vchStatus. I need to generate a table with every distinct client in the records and the count for each status of that client. What I have tried is:
Controller:
public ActionResult ReportesClient(String fromDay = null, String toDay = null)
{
if (fromDay != string.Empty && toDay != String.Empty)
{
return View(entidad.tblKaizens.Select(r => r.vchClient).Distinct());
}
else if (fromDay == null || toDay == null || fromDay == String.Empty || toDay == String.Empty)
{
return View(entidad.tblKaizens.Where((r => r.vchStatus.Equals("Something"))).ToList());
}
}
View:
<table>
<tr>
<th>
Celula
</th>
<th>
Total
</th>
<th>
Implementado
</th>
<th>
No viable
</th>
<th>
Transferido
</th>
<th>
En revision
</th>
<th>
Sin revisar
</th>
<th>
Duracion Promedio
</th>
</tr>
<tr>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.vchClient)
</td>
<td>
#Model.Count()
</td>
<td>
#Model.Where(r => r.vchStatus.Equals("Implementado")).Count()
</td>
<td>
#Model.Where(r => r.vchStatus.Equals("No viable")).Count()
</td>
<td>
#Model.Where(r => r.vchStatus.Equals("Transferido")).Count()
</td>
<td>
#Model.Where(r => r.vchStatus.Equals("En revision")).Count()
</td>
<td>
#Model.Where(r => r.vchStatus.Equals("Sugerido")).Count()
</td>
<td>
#Model.Average(r => r.intDuration)
</td>
</tr>
}
I thought this could work but it is not, I got all the records not by client and the same with count and average is from all the records in the model between the dates selected by the user.

Related

View in ASP.NET Core 5 MVC display as required

I am creating an ASP.NET Core 5 MVC web app and I have a problem.
View only display the same value.
I have debug and the controller seem to be ok.
But I don't have any idea why the view is only display 9 row and all rows are the same.
Here is the code in the controller:
public class MatchesController : Controller
{
private ISender _mediator;
public MatchesController(ISender mediator)
{
_mediator = mediator;
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> ViewAllMatch()
{
var matchQuery = await _mediator.Send(new GetMatchesDetail());
// ReUse model from Apllication Layer
// Manual Mapping from matches to MatchViewModel
// GetMatchesDetail : IRequest<IEnumerable<MatchesDetail>>
// Manual Mapping IEnumerable<MatchesDetail> =>IEnumerable<MatchViewModel>
MatchViewModel matchesvm = new MatchViewModel();
List<MatchViewModel> retList = new List<MatchViewModel>();
// IEnumerable<MatchesDetail> retList;
foreach (var item in matchQuery)
{
matchesvm.MatchId = item.MatchId;
matchesvm.MatchNumber = item.MatchNumber;
matchesvm.DateMatch = item.DateMatch;
matchesvm.TimeMatch = item.TimeMatch;
matchesvm.MatchYear = item.MatchYear;
matchesvm.SeasonId = item.SeasonId;
matchesvm.SeasonName = item.SeasonName;
matchesvm.Round = item.Round;
matchesvm.Stage = item.Stage;
matchesvm.SubStage = item.SubStage;
matchesvm.HTeam = item.HTeam;
matchesvm.HGoal = item.HGoal;
matchesvm.HTeamCode = item.HTeamCode;
matchesvm.GGoal = item.GGoal;
matchesvm.GTeam = item.GTeam;
matchesvm.GTeamCode = item.GTeamCode;
matchesvm.WinNote = item.WinNote;
matchesvm.Stadium = item.Stadium;
matchesvm.Referee = item.Referee;
matchesvm.Visistors = item.Visistors;
retList.Add(matchesvm);
}
return View(retList);
}
}
And here is the view:
#model IEnumerable<MatchViewModel>
#{
ViewData["Title"] = "ViewAllMatch";
string flag1, flag2;
}
<h1>ViewAllMatch</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.MatchId)
</th>
<th>
#Html.DisplayNameFor(model => model.MatchNumber)
</th>
<th>
#Html.DisplayNameFor(model => model.DateMatch)
</th>
<th>
#Html.DisplayNameFor(model => model.TimeMatch)
</th>
<th>
#Html.DisplayNameFor(model => model.MatchYear)
</th>
<th>
#Html.DisplayNameFor(model => model.SeasonId)
</th>
<th>
#Html.DisplayNameFor(model => model.SeasonName)
</th>
<th>
#Html.DisplayNameFor(model => model.Round)
</th>
<th>
#Html.DisplayNameFor(model => model.Stage)
</th>
<th>
#Html.DisplayNameFor(model => model.SubStage)
</th>
<th>
#Html.DisplayNameFor(model => model.HTeam)
</th>
<th>
#Html.DisplayNameFor(model => model.HTeamCode)
</th>
<th>
#Html.DisplayNameFor(model => model.HGoal)
</th>
<th>
#Html.DisplayNameFor(model => model.GGoal)
</th>
<th>
#Html.DisplayNameFor(model => model.GTeam)
</th>
<th>
#Html.DisplayNameFor(model => model.GTeamCode)
</th>
<th>
#Html.DisplayNameFor(model => model.WinNote)
</th>
<th>
#Html.DisplayNameFor(model => model.Stadium)
</th>
<th>
#Html.DisplayNameFor(model => model.Referee)
</th>
<th>
#Html.DisplayNameFor(model => model.Visistors)
</th>
<th>
#Html.DisplayNameFor(model => model.Status)
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.MatchId)
</td>
<td>
#Html.DisplayFor(modelItem => item.MatchNumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateMatch)
</td>
<td>
#Html.DisplayFor(modelItem => item.TimeMatch)
</td>
<td>
#Html.DisplayFor(modelItem => item.MatchYear)
</td>
<td>
#Html.DisplayFor(modelItem => item.SeasonId)
</td>
<td>
#Html.DisplayFor(modelItem => item.SeasonName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Round)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stage)
</td>
<td>
#Html.DisplayFor(modelItem => item.SubStage)
</td>
<td class="text-center">
#Html.DisplayFor(modelItem => item.HTeam)
</td>
<td>
#{
flag1 = "/img/Team/"+#item.HTeamCode+".png";
flag2= "/img/Team/" + #item.GTeamCode + ".png";
}
<img src="#flag1" />
#*#Html.DisplayFor(modelItem => item.HTeamCode)*#
</td>
<td>
#Html.DisplayFor(modelItem => item.HGoal)
</td>
<td>
#Html.DisplayFor(modelItem => item.GGoal)
</td>
<td>
#Html.DisplayFor(modelItem => item.GTeam)
</td>
<td>
#Html.DisplayFor(modelItem => item.GTeamCode)
</td>
<td>
#Html.DisplayFor(modelItem => item.WinNote)
</td>
<td>
#Html.DisplayFor(modelItem => item.Stadium)
</td>
<td>
#Html.DisplayFor(modelItem => item.Referee)
</td>
<td>
#Html.DisplayFor(modelItem => item.Visistors)
</td>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
#Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
</td>
</tr>
}
</tbody>
</table>
The reason why you always get the same value is your MatchViewModel is new outside the foreach method. This means you always added the same MatchViewModel and its value has been changed during each foreach. This will make the List view model always added the same viewmodel like your view shows always the same row.
I suggest you could try below codes:
public async Task<IActionResult> ViewAllMatch()
{
var matchQuery = await _mediator.Send(new GetMatchesDetail());
//ReUse model from Apllication Layer
//Manual Mapping from matches to MatchViewModel
//GetMatchesDetail : IRequest<IEnumerable<MatchesDetail>>
//Manual Mapping IEnumerable<MatchesDetail> =>IEnumerable<MatchViewModel>
List<MatchViewModel> retList = new List<MatchViewModel>();
//IEnumerable<MatchesDetail> retList;
foreach (var item in matchQuery)
{
MatchViewModel matchesvm = new MatchViewModel();
#region ManualMapping
matchesvm.MatchId = item.MatchId;
matchesvm.MatchNumber = item.MatchNumber;
matchesvm.DateMatch = item.DateMatch;
matchesvm.TimeMatch = item.TimeMatch;
matchesvm.MatchYear = item.MatchYear;
matchesvm.SeasonId = item.SeasonId;
matchesvm.SeasonName = item.SeasonName;
matchesvm.Round = item.Round;
matchesvm.Stage = item.Stage;
matchesvm.SubStage = item.SubStage;
matchesvm.HTeam = item.HTeam;
matchesvm.HGoal = item.HGoal;
matchesvm.HTeamCode = item.HTeamCode;
matchesvm.GGoal = item.GGoal;
matchesvm.GTeam = item.GTeam;
matchesvm.GTeamCode = item.GTeamCode;
matchesvm.WinNote = item.WinNote;
matchesvm.Stadium = item.Stadium;
matchesvm.Referee = item.Referee;
matchesvm.Visistors = item.Visistors;
#endregion
retList.Add(matchesvm);
}
return View(retList);
//return View(matches); //IEnumerable<MatchesDetail>
}
You have bug in your code, you should create a new MatchViewModel instance for each item inside of the loop
foreach (var item in matchQuery)
{
var matchesvm = new MatchViewModel();
matchesvm.MatchId = item.MatchId;
... and so on
}
but you can use Linq instead foreach loop
var retList= matchQuery.Select(item=> new MatchViewModel
{
MatchId = item.MatchId;
MatchNumber = item.MatchNumber;
DateMatch = item.DateMatch
.... and so on
}).ToList();
return View(retList);
IMHO no much sense to convert IEnumerable of MatchesDetail => IEnumerable of MatchViewModel since they have the same property names and you use them only to display data. You can use matchQuery directly
return View(matchQuery);
and view
#model IEnumerable<MatchesDetail>

How can I compare the session value to current user id [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How can I compare the session value with the current user id.
To display only the record of that user who is login.
Here I join three tables to make this view.
if (Session["UserId"] == Model.userAccount.UserId) Here is the code where I got error.
code:
#model IEnumerable<OnlineTaxiReservationSystem.Models.BookingViewModel>
#{
ViewBag.Title = "Index";
}
<h2 id="h1">Bookings Detail</h2>
<p>
#Html.ActionLink("Add New Booking", "Create")
</p>
#if (Session["User"] == null)
{
Response.Redirect(Url.Action("Login", "Account"));
}
else if (Session["LoginRole"].ToString() == "Admin")
{
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.taxi.TaxiName)
</th>
<th>
#Html.DisplayNameFor(model => model.userAccount.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.BookingStartDateandTime)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.BookingEndDateandTime)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.UserContactNo)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.UserStartingLoaction)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.UserDistination)
</th>
<th>
#Html.DisplayNameFor(model => model.bookingStatus.Status)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.taxi.TaxiName)
</td>
<td>
#Html.DisplayFor(modelItem => item.userAccount.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.BookingStartDateandTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.BookingEndDateandTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.UserContactNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.UserStartingLoaction)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.UserDistination)
</td>
<td>
#Html.DisplayFor(modelItem => item.bookingStatus.Status)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.booking.BookingId }) |
#Html.ActionLink("Details", "Details", new { id = item.booking.BookingId }) |
#Html.ActionLink("Delete", "Delete", new { id = item.booking.BookingId })
</td>
</tr>
}
</table>
}
else if (Session["UserId"] == Model.userAccount.UserId)
{
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.taxi.TaxiName)
</th>
<th>
#Html.DisplayNameFor(model => model.userAccount.FirstName)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.BookingStartDateandTime)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.BookingEndDateandTime)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.UserContactNo)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.UserStartingLoaction)
</th>
<th>
#Html.DisplayNameFor(model => model.booking.UserDistination)
</th>
<th>
#Html.DisplayNameFor(model => model.bookingStatus.Status)
</th>
<th></th>
</tr>
#foreach (var item in #Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.taxi.TaxiName)
</td>
<td>
#Html.DisplayFor(modelItem => item.userAccount.FirstName)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.BookingStartDateandTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.BookingEndDateandTime)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.UserContactNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.UserStartingLoaction)
</td>
<td>
#Html.DisplayFor(modelItem => item.booking.UserDistination)
</td>
<td>
#Html.DisplayFor(modelItem => item.bookingStatus.Status)
</td>
</tr>
}
</table>
}
Model is an enumeration of BookingViewModel, so this statement will fail:
else if (Session["UserId"] == Model.userAccount.UserId)
Because Model is a group of items, not just one item.
What you need to do instead is look for the user account and display it:
//This is the else statement that was giving you trouble. Change it to:
else
{
string sessionId = Session["UserId"] as string;
var account = Model.FirstOrDefault(a => a.userAccount.UserId == sessionId);
if(account != null)
{
//Use your display code here
}
else
{
//Figure out what to do if for some odd reason, you can't find it.
}
}
The quick code sample assumes userId is a string. Use casting as needed.
I would suggest creating a partial view to display the fields as opposed to code repetition.
You can do it through the User property of your ViewPage. Do not save user ID in session
Suppose you have authenticated user and he has role & identity.
public class MyPrincipal: IPrincipal
{
private MyIdentity _identity;
private string[] _roles;
public MyPrincipal(MyIdentity identity, string[] roles)
{
_identity = identity;
_roles = roles;
}
public bool IsInRole(string role)
{
return _roles?.Length > 0 && _roles.Contains(role);
}
IIdentity IPrincipal.Identity
{
get
{
return _identity;
}
}
public MyIdentity Identity
{
get { return _identity;}
}
// User ID you need
public int UserId
{
get
{
return Identity.UserId;
}
}
}
somewhere in your code
#{
int currentUserId = (User as MyPrincipal).UserId;
}
#if (currentUserId == Model.userId)
{
// this will be executed personally per executing user
}

C# MVC filtering a table via a dropdown

I have a view which has a table that displays values. I would like to add a drop down that filters the results of the status column. So for example if I select "Reviewing", only records with the "Reviewing" status will appear. Below I have my view.
#model IEnumerable<DRT.Models.Master>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.RequestType.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Reviewer.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.Status.Name)
</th>
<th>
#Html.DisplayNameFor(model => model.ProjectName)
</th>
<th>
#Html.DisplayNameFor(model => model.ProjectLocation)
</th>
<th>
#Html.DisplayNameFor(model => model.Requestor)
</th>
<th>
#Html.DisplayNameFor(model => model.DateReceived)
</th>
<th>
#Html.DisplayNameFor(model => model.ReviewCompDate)
</th>
<th>
#Html.DisplayNameFor(model => model.ProjectFolerLink)
</th>
<th>
#Html.DisplayNameFor(model => model.ModellingReferralDate)
</th>
<th>
#Html.DisplayNameFor(model => model.ModellingReviewCompletionDate)
</th>
<th>
#Html.DisplayNameFor(model => model.SewerMaintenanceReferralDate)
</th>
<th>
#Html.DisplayNameFor(model => model.SewerMaintenanceReviewCompletionDate)
</th>
<th>
#Html.DisplayNameFor(model => model.FlowControlReferralDate)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.RequestType.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Reviewer.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Status.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProjectName)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProjectLocation)
</td>
<td>
#Html.DisplayFor(modelItem => item.Requestor)
</td>
<td>
#Html.DisplayFor(modelItem => item.DateReceived)
</td>
<td>
#Html.DisplayFor(modelItem => item.ReviewCompDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProjectFolerLink)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProjectComments)
</td>
<td>
#Html.DisplayFor(modelItem => item.DischargeLocationID)
</td>
<td>
#Html.DisplayFor(modelItem => item.DischargeDistrict)
</td>
<td>
#Html.DisplayFor(modelItem => item.SystemType)
</td>
<td>
#Html.DisplayFor(modelItem => item.AffectedRequlator)
</td>
<td>
#Html.DisplayFor(modelItem => item.StartDateOfDischarge)
</td>
<td>
#Html.DisplayFor(modelItem => item.DurationOfDischarge)
</td>
<td>
#Html.DisplayFor(modelItem => item.RequestFlowRate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ApprovedDischargeRate)
</td>
<td>
#Html.DisplayFor(modelItem => item.DischargeLocationComments)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModellingReferralDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ModellingReviewCompletionDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.SewerMaintenanceReferralDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.SewerMaintenanceReviewCompletionDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.FlowControlReferralDate)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.MasterId }) |
#Html.ActionLink("Details", "Details", new { id=item.MasterId }) |
#Html.ActionLink("Delete", "Delete", new { id=item.MasterId })
</td>
</tr>
}
</table>
You basically need a SELECT element in your view and when use selects something, you may post the selected value to the action method where you will use this value when getting data from your db.
You may create a new view model
public class ListAndSearchVm
{
public IEnumerable<DRT.Models.Master> Data { set;get;}
public IEnumerable<SelectListItem> Statuses { set;get;}
public string SelectedStatus { set;get;}
}
Now in your GET action, you need to load the Statuses property of the view model and the Data property. Have a parameter to accept the selected option value from your status filter dropdown. Based on the value of this parameter get the filtered data.
public ActionResult Index(string selectedStatus="")
{
var vm= new ListAndSearchVm();
//Hard coding 2 items for demo. You can read this from your db table
vm.Statuses = new List<SelectListItem> {
new SelectListITem { Value="Open", Text="Open"},
new SelectListITem { Value="Closed", Text="Closed"},
};
var data= dbContext.Masters;
if(!String.IsNullOrEmpty(selectedStatus))
{
data = data.Where(f=>f.Status.Name==selectedSatatus);
}
vm.Data = data.ToList();
return View(vm);
}
Now in your view
#model ListAndSerchVm
#using(Html.BeginForm("Index","YourControllerName",FormMethod.Get))
{
<label> Select a status</label>
#Html.DropDownListFor(f=>f.SelectedStatus,Model.Statuses,"Select")
<input type="submit" value="Filter" />
}
<table>
<tr>
<th>Request type</th>
<th>Reviewer</th>
<th>Status</th>
</tr>
#foreach(var item in Model.Data)
{
<tr>
<td>#item.RequestType.Name)</td>
<td>#item.Reviewer.Name</td>
<td>#item.Status.Name</td>
</tr>
}
</table>

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());

Change background color of highest number in a table

I have a table, and I want the highest number in each column to have a yellow background. What would be the best way to achieve this?
Here you can see the code of my table:
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Naam)
</th>
<th>
#Html.DisplayNameFor(model => model.Bier)
</th>
<th>
#Html.DisplayNameFor(model => model.Frisdrank)
</th>
<th>
#Html.DisplayNameFor(model => model.Chips)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.ActionLink(item.Naam, "Details", new { id = item.Id })
</td>
<td>
#Html.DisplayFor(modelItem => item.Bier)
#{AantalBier += item.Bier;}
</td>
<td>
#Html.DisplayFor(modelItem => item.Frisdrank)
#{AantalFrisdrank += item.Frisdrank;}
</td>
<td>
#Html.DisplayFor(modelItem => item.Chips)
#{AantalChips += item.Chips;}
</td>
</tr>
}
<tr style="background-color:#d9e6c4">
<th>
Totaal
</th>
<th>
#AantalBier
</th>
<th>
#AantalFrisdrank
</th>
<th>
#AantalChips
</th>
</tr>
</table>
I would sort the model using the OrderBy linq extension method, then the highest value would come to the top of the table and I'd highlight the first row.
However if you don't want to sort the model for some reason, you can check if the value is the max by calling the Max method.
For instance:
foreach (var i in Model){
if (i.Number == Model.Max(x=> x.Number))
<td style="background:yellow">Something</td>
else
<td>Something</td>
}

Categories

Resources