Display a single item from the Model in the same view as a list - asp.net-mvc

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.

Related

How to put a row in red with Razor in MVC5

I have a table, which I want to mark when the real stock is less than the minimum stock, for that I'm going to render the view using Razor, but I'm not getting results
I enclose my view that contains the table that we wish to intervene
<table class="table table-bordered table-hover table-condensed">
<tr>
<th>
#Html.DisplayNameFor(model => model.First().v_Nombre)
</th>
<th>
#Html.DisplayNameFor(model => model.First().StockReal)
</th>
<th>
#Html.DisplayNameFor(model => model.First().StockMinimo)
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
var fila = string.Empty;
if(item.StockReal < item.StockMinimo)
{
fila = "danger";
}
<tr class="#fila">
<td>
#Html.DisplayFor(modelItem => item.v_Nombre)
</td>
<td>
#Html.DisplayFor(modelItem => item.StockReal)
</td>
<td>
#Html.DisplayFor(modelItem => item.StockMinimo)
</td>
<td>
Editar
</td>
</tr>
}
</table>
Expected behavior: that the row where the real stock is less than the minimum stock turns red
Behavior Achieved: No change in my view
what am I doing wrong? What is missing in my Razor code? any help for me?
In bootstrap 4 the class is table-danger so change your code:
if(item.StockReal < item.StockMinimo)
{
fila = "table-danger";
}

Object reference not set to an instance of an object. First login. solved when I rerun the application?

When I create a user and he's logged in for the first time I get this error when I try to display(View details) the requests that he created so far(0 request created). Once I rerun the application again with the same user the view works and I get no error. Here's the line that's generates the error:
#foreach (var item in Model)`
Here's the view that generates the error:
#model IEnumerable<Mutuelle.Domain.Entities.Demande>
#{
ViewBag.Title = "DemandesAdherent";
}
#{
Layout = "~/Views/Shared/_LayoutAdherent.cshtml";
}
<h2>DemandesAdherent</h2>
<p>
#Html.ActionLink("CreƩr nouvelle", "AddDemande")
</p>
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.Date)
</th>
<th>
#Html.DisplayNameFor(model => model.nomBeneficiaire)
</th>
<th>
#Html.DisplayNameFor(model => model.Nomrubrique)
</th>
<th>
#Html.DisplayNameFor(model => model.etat)
</th>
<th></th>
</tr>
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.Date)
</td>
<td>
#Html.DisplayFor(modelItem => item.nomBeneficiaire)
</td>
<td>
#Html.DisplayFor(modelItem => item.Nomrubrique)
</td>
<td>
#Html.DisplayFor(modelItem => item.etat)
</td>
<td>
#Html.ActionLink("Details", "DetailsDemande", new { id=item.DemandeId }) #*|
#Html.ActionLink("Delete", "DeleteDemande", new { id=item.DemandeId })*#
</td>
</tr>
}
</table>

How can I get value textbox inside partial view in asp.net mvc

How can I get value of #Html.TextBoxFor(modelItem => item.Sales) in partial view after pass to controller
My partial view :
#model IEnumerable<PG.Admin.Models.Items.ItemViewModel>
<table>
<tr>
<th>
#Html.DisplayNameFor(model => model.First().ItemId)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Name)
</th>
<th>
#Html.DisplayNameFor(model => model.First().Sales)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.ItemId)
</td>
<td>
#Html.DisplayFor(modelItem => item.Name)
</td>
<td>
#Html.TextBoxFor(modelItem => item.Sales)
</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>

How can I insert mulitple rows in table at once in MVC asp .net?

I am new at MVC asp .net.
I have a grid in which I am adding rows.
Now I want to perform mulitple rows insertion in a table at once.
How can I do that. SO multiple rows will be added in MVC.
Any help would be appericiated.
You want to learn how to ModelBind to a Collection. There are many answers here already for this technique.
https://stackoverflow.com/search?q=model+binding+to+a+collection
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Do you mean something like this?.
<table class="table" style="vertical-align: middle;">
<thead>
<tr>
<th>
#Html.DisplayNameFor(model => model.title)
</th>
<th style="text-align: center;">
#Html.DisplayNameFor(model => model.imgAr)
</th>
<th>
#Html.DisplayNameFor(model => model.created)
</th>
<th></th>
</tr>
</thead>
<tbody id="tblOfContent">
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.title)
</td>
<td style="text-align: center;">
<img src="~/newsImages/#item.imgAr" class="img-thumbnail" />
</td>
<td>
#item.created.ToShortDateString()
</td>
<td>
#Html.ActionLink("Details", "Edit", new { id = item.id }) |
#Html.ActionLink("Details", "Details", new { id = item.id })
</td>
</tr>
}
</tbody>
</table>
and in the Controller something like this:
public ActionResult Index()
{
return View(newsService.GetAll().OrderByDescending(x => x.created));
}

Resources