Do a sum in View based on criteria - asp.net-mvc

I have a Model and view something like this:
Model and View
I want to display the sum/total based on either State or Federal (see the green box in the image).
Here is my view so far.
#if (Model.Any())
{
<table>
<tr>
<th>
</th>
<th>
STATE
</th>
<th>
FEDERAL
</th>
</tr>
<tr>
<th width="180">
Project
</th>
<th width="120">
Sponsor
</th>
<th width="120">
Sponsor
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(m => item.Project)
</td>
#if (item.Provider == "State")
{
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
<td>
</td>
}
else
{
<td>
</td>
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
}
</tr>
}
</table>
}
How can I display the value (in the green box)? I know that doing a sum in View is not a good practice.

You can use linq with this.
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(m => item.Project)
</td>
#if (item.Provider == "State")
{
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
<td>
</td>
}
else
{
<td>
</td>
<td>
#Html.DisplayFor(m => item.Sponsor)
</td>
}
</tr>
}
<tr>
<td></td>
<td>#Model.Where(x => x.Provider == "State").Sum(x => x.Sponsor)</td>
<td>#Model.Where(x => x.Provider != "State").Sum(x => x.Sponsor)</td>
</tr>

Related

Display a single item from the Model in the same view as a list

I want to display a search result using this InstitutionName as a query, but instead of appearing in the resulting table repeatedly on every row, I just want to show it as a heading. I tried using DisplayFor but it always returns an error, I'm guessing because my model is an IEnumerable, but I don't know, I just put in DisplayNameFor temporarily to simulate the output I want to show, which should be the value of the property itself and not just the property name.
I've read about using a ViewModel but I just want to know if I can accomplish this with what I have right now.
View
#model IEnumerable<SMOSystemv2.Models.Tender>
#{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>#Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)</h2>
<table class="table">
<tr>
<th nowrap>
#Html.DisplayNameFor(model => model.Compendium.Institution.InstitutionName)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.TotalAmount)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td nowrap>
#Html.DisplayFor(modelItem => item.Compendium.Institution.InstitutionName)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.TotalAmount)
</td>
</tr>
}
</table>
This is the kind of display that I was initially looking for using #Model.First() as suggested, but implementing a ViewModel instead is highly recommended
#model IEnumerable<SMOSystemv2.Models.Tender>
#{
ViewBag.Title = "Tenders";
}
<h1>Tender History</h1>
<h2>#Model.First().Compendium.Institution.InstitutionName</h2>
<table class="table">
<tr>
<th nowrap>
#Html.DisplayNameFor(model => model.Compendium.Qualification.Title)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Slots)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.TotalAmount)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.Semester)
</th>
<th nowrap>
#Html.DisplayNameFor(model => model.DateReceived)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<tr>
<td nowrap>
#Html.DisplayFor(modelItem => item.Compendium.Qualification.Title)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Slots)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.TotalAmount)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.Semester)
</td>
<td nowrap>
#Html.DisplayFor(modelItem => item.DateReceived)
</td>
<td nowrap>
#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>
This property returns the first option of the list or option selected as the default
FirstOrDefault
#if(Model != null){
<h2>#Model.FirstOrDefault().Compendium.Institution.InstitutionName</h2>
}
Try following solution:
<h2>#Html.DisplayNameFor(#Model.FirstOrDefault().Compendium.Institution.InstitutionName)</h2>
instead of
<h2>#Html.DisplayNameFor(model=>model.Compendium.Institution.InstitutionName)</h2>
because you are using #model IEnumerable<SMOSystemv2.Models.Tender> at your code. The property FirstOrDefault() will give you the first value in the list as default value.

View renders but doesn't update

Ive got a view that i pass a model to. I want it to render out a table out of a list in the model. It does that just fine it renders the view in that case that if i debugg it i can se the values goes to the right places. But the browser never updates. If i press ctrl+R in the browser it renders the view again in the exact same way but this time it show the list in the browser. Can i make this update everytime the view renders so that i don't have to press ctrl+R?
[HttpPost]
public ActionResult CreateResource(ViewModel view)
{
view.ResourceDateCreated = DateTime.Now;
viewmodel = view;
Session["ViewModel"]=viewmodel;
return View("Index", viewmodel);
}
The view:
<table class="table">
<tr>
<th>
MetaDataName
</th>
<th>
MetaDataDescription
</th>
<th>
LanguageCode
</th>
<th>
UserId
</th>
<th>
MetaDataDateCreated
</th>
#*<th>#Html.DisplayNameFor(model => model.MetaList)</th>*#
</tr>
#if (Model.Metadata.Count != 0)
{
foreach (var item in Model.Metadata)
{
<tr>
<td>
#item.MetaDataName
</td>
<td>
#item.MetaDataDescription
</td>
<td>
#item.LanguageCode
</td>
<td>
#item.UserId
</td>
<td>
#item.MetaDataDateCreated.ToString()
</td>
<td>
<table>
<tr>
<th>
MetaListId
</th>
</tr>
#if (item.MetaList.Count != 0)
{
foreach (var list in item.MetaList)
{
<tr>
<td>
#list.MetaListId
</td>
</tr>
}
}
</table>
</td>
</tr>
}
}
</table>

Templates can be used only with field access, property access in Razor

I got this error when I was creating a Edit and Templating for Product.
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
Edit In my Razor View:
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.Select(m => m.ProductID))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.Name))
</th>
<th>
#Html.DisplayFor(model => model.Select(m => m.ListPrice))
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.ListPrice)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id=item.ProductID }) |
#Html.ActionLink("Details", "Details", new { id=item.ProductID }) |
#Html.ActionLink("Delete", "Delete", new { id=item.ProductID })
</td>
</tr>
}
</table>
Do you know what that mean and how to fix it? Thank you so much!
doesnt this just work?
<th> #Html.DisplayFor(model => model.ProductID) </th>
edit, try something like this:
<table id="myTable">
<tr>
<th>
#Html.DisplayNameFor(model => model.ProductID)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
<td>
#Html.DisplayFor(modelItem => item.ProductID)
</td>
</tr>
}
</table>

LINQ query not working properly

I have MVC application and I have two entities.
Entity Lead inherits from Customer.
Now, In Lead controllers index view, I have written below code...
public ViewResult Index()
{
var Leads = (from c in db.Customers.OfType<CRMEntities.Lead>()
where(c.IsActive == true) select c);
return View(Leads.ToList());
}
and I have the code below in the index view.
#model IEnumerable<CRMEntities.Lead>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
.
.
Records exist in the table it not returning any record.
Is query right?
Full View Code
#model PagedList.IPagedList<CRMEntities.Lead>
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
#Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
Status
</th>
<th>
IsQualified
</th>
<th>
Name
</th>
<th>
Address
</th>
<th>
OfficePhone1
</th>
<th>
Website
</th>
<th>
Email2
</th>
<th>
Email1
</th>
<th>
FaxNo
</th>
<th>
Remark
</th>
<th>
Rating
</th>
<th>
BusinessType
</th>
<th>
IsActive
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Status)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsQualified)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Address)
</td>
<td>
#Html.DisplayFor(modelItem => item.OfficePhone1)
</td>
<td>
#Html.DisplayFor(modelItem => item.Website)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email2)
</td>
<td>
#Html.DisplayFor(modelItem => item.Email1)
</td>
<td>
#Html.DisplayFor(modelItem => item.FaxNo)
</td>
<td>
#Html.DisplayFor(modelItem => item.Remark)
</td>
<td>
#Html.DisplayFor(modelItem => item.Rating)
</td>
<td>
#Html.DisplayFor(modelItem => item.BusinessType)
</td>
<td>
#Html.DisplayFor(modelItem => item.IsActive)
</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>
Query looks fine from what can be seen.
The view code you are showing will not display anything. Write out #Model.Count() in the view to see how many records you have.
If you don't have a database profiler, I'd recommend getting one.

Is there a way to make the MVC Html.Actionlink invisible based on a property value

Hi say I have a view containing:
<table>
<tr>
<th>
Invoice Number
</th>
<th>
Invoice Date
</th>
<th>
Organisation
</th>
<th>
Region
</th>
<th>
Total Excluding GST
</th>
<th>
Total Including GST
</th>
<th>
</th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.InvoiceNumber)
</td>
<td>
#Html.DisplayFor(modelItem => item.InvoiceDate, "{0:D}")
</td>
<td>
#Html.DisplayFor(modelItem => item.Organisation.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.Area.Name)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalExcludingGst)
</td>
<td>
#Html.DisplayFor(modelItem => item.TotalIncludingGst)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Id })
</td>
</tr>
}
</table>
and each item has a boolean property Editable. Is there a way to make the Edit link visible/invisible based on the value of this property?
You can use an if:
#if (item.Editable) {
#Html.ActionLink(...)
}
Basically you can use css. But if you insist in actionlink
Html.ActionLink(
"LinkName",
"Action",
null,
new { #style = "display:none" });
So if you use it with #if statement in view, you can achieve what you want.
Use CSS
I am invisible

Resources