I want to display summary totals in a table-footer WITHOUT creating a special property in the view-model.
However...the footer code fails, saying:
"Templates can be used only with field access, property access,
single-dimension array index, or single-parameter custom indexer
expressions."
FOOTER CODE LOOKS LIKE:
<tfoot>
<tr>
<td colspan="6" class="bg-gray"></td>
</tr>
<tr>
<td colspan="2">
Totals DO NOT include Price Consessions
</td>
<td>
#Html.DisplayFor(modelItem => Model.Entities.Sum( x => x.Price))
</td>
<td>
#Html.DisplayFor(modelItem => Model.Entities.Sum(x => x.AnnualFee))
</td>
<td>
</td>
</tr>
</tfoot>
Just leave out the #Html.DisplayFor and put #Model.Entities.Sum( x => x.Price)
Related
I have a table which will be a Master Color Table and therefore I will list around 1800 Colors by PMS number, and will also have the Color Hex for each PMS number. I want to display the Color dependent on the Hex in the cell that contains the HEX. How should I go about this?
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Colour_MasterPms)
</td>
<td>
#Html.DisplayFor(modelItem => item.Colour_Master_Family)
</td>
<td>
#Html.DisplayFor(modelItem => item.ColorHex)
</td>
<td>
#Html.DisplayFor(modelItem => item.Ink)
</td>
<td>
#Html.DisplayFor(modelItem => item.Paint)
</td>
<td>
<a asp-action="Edit" asp-route-id="#item.Id"> <i class="bi bi-`pencil-square"></i></a>  |`
<a asp-action="Delete" asp-route-id="#item.Id"><i class="bi bi-trash"></i></a>
</td>
</tr>
}
</tbody>
I know there's other questions about this, but I want a better explanation about it.
I want to show a table with my Model.List and after POST I still need to access that list. Today this is not happening.
This is my cshtml:
#foreach (var student in Model.ListSchedulingDetails)
{
<tr>
<td width="150">
#Html.DisplayFor(modelItem => student.SchedulingDate)
</td>
<td width="250">
#Html.DisplayFor(modelItem => student.TeacherName)
</td>
<td width="250">
#Html.DisplayFor(modelItem => student.StudentName)
</td>
<td width="150">
#Html.DisplayFor(modelItem => student.SchedulingHour)
</td>
<td width="250">
#Html.DisplayFor(modelItem => student.SchedulingObservation)
</td>
</tr>
}
After POST, my Model.ListSchedulingDetails is null. What's happening?
There are two things here:
DisplayFor() is for only displaying. For posting, we need input
elements like HiddenFor(),TextBoxFor()
you would need to index them for posting back in case
of collections, foreach wouldn't help
Your code would need to be like:
#for (int i=0; i< Model.ListSchedulingDetails.Count; i++)
{
<tr>
<td width="150">
#Html.DisplayFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
#Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
</td>
<td width="250">
#Html.DisplayFor(modelItem => ListSchedulingDetails[i].TeacherName)
#Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
</td>
<td width="250">
#Html.DisplayFor(modelItem => ListSchedulingDetails[i].StudentName)
#Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
</td>
<td width="150">
#Html.DisplayFor(modelItem => ListSchedulingDetails[i].SchedulingHour)
#Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
</td>
<td width="250">
#Html.DisplayFor(modelItem => ListSchedulingDetails[i].SchedulingObservation)
#Html.HiddenFor(modelItem => ListSchedulingDetails[i].SchedulingDate)
</td>
</tr>
}
I have a form, that just runs through a model and returns all the rows,. pretty basic MVC one:
#model IEnumerable<Something.Else.Model>
<table>
<thead>
<tr>
<th>#Html.DisplayNameFor()</th>
<th>#Html.DisplayNameFor()</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.)</td>
<td>#Html.DisplayFor(modelItem => item.)</td>
</tr>
}
</tbody>
</table>
However, what I want is to load an editor row when a button is clicked, and the editor fields to reference the model because of the validation I have on the model.
#model IEnumerable<Something.Else.Model>
<table>
<thead>
<tr>
<th>#Html.DisplayNameFor()</th>
<th>#Html.DisplayNameFor()</th>
</tr>
</thead>
<tbody>
<tr style="display: none"> // editor row
<td>#Html.EditorFor()</td>
<td>#Html.TextBox()</td>
</tr>
#foreach (var item in Model)
{
<tr>
<td>#Html.DisplayFor(modelItem => item.)</td>
<td>#Html.DisplayFor(modelItem => item.)</td>
</tr>
}
</tbody>
</table>
The issue I am having is that the form is Enumerable, so when I try to load a single row i get the message IEnumerable does not contain a definition for "field name" and no extension method etc etc
I know I could use #Html.Editor instead of editor for, but I have a lot of validation on the model I want to use. I could put these rows in the #foreach section and load the top one and use jquery to clear the values but that seems like a bad way.
The purpose of this is to enter a new line, not edit an existing one.
What is the most efficient way of achieving this?
One way would be to replace foreach with for with indexing of items, add a placeholder (empty) item at the end of the collection and display the editing item on button click:
#model List<Something.Else.Model>
<table>
<thead>
<tr>
<th>#Html.DisplayNameFor(m => m.First)</th>
<th>#Html.DisplayNameFor(m => m.Second)</th>
</tr>
</thead>
<tbody>
<tr id="edit-item" style='display: none'>
<td>
#Html.EditorFor(m => Model[Model.Count - 1].First)
</td>
<td>
#Html.EditorFor(m => Model[Model.Count - 1].Second)
</td>
</tr>
#for (var i = 0; i < Model.Count - 1; i++) // the last item is the placeholder (editing item)
{
<tr>
<td>
#Html.DisplayFor(m => Mode[i].First)
#Html.HiddenFor(m => Mode[i].First)
</td>
<td>
#Html.DisplayFor(m => Mode[i].Second)
#Html.HiddenFor(m => Mode[i].Second)
</td>
</tr>
}
</tbody>
</table>
Model type is changed to List to allow indexing of items.
The controller would need to determine from the values in the placeholder if the placeholder is filled (an item was entered).
I have a table in my view with columns of numerical data. In the footer of my table I have a row containing the totals of each column. The totals are adding up correctly, but I want the vales in them to be rounded to 2 decimal places, eg instead of 47.4386, I want it to display 47.44.
Sample code:
<table class="display" id="dailyreporttable">
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.DailyReportDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.Estate)
</td>
<td>
#Html.DisplayFor(modelItem => item.BettingShop)
</td>
<td class="alignRight">
#Html.DisplayFor(modelItem => item.ShopBalance)
</td>
<td class="alignRight">
#Html.DisplayFor(modelItem => item.TotalCashIn)
</td>
}
<tfoot>
<tr>
<td>Total</td>
<td></td>
<td></td>
<td class="alignRight">#Model.Sum(item => item.ShopBalance)</td>
<td class="alignRight">#Model.Sum(item => item.TotalCashIn)</td>
</tr>
</tfoot>
Try this:
<td class="alignRight">#Model.Sum(item => item.ShopBalance).ToString("N2")</td>
Got it in the end, the values were actually nullable decimals so I had to do:
<td class="alignRight">#Model.Sum(item => item.ShopBalance).Value.ToString("N2")</td>
Try:
<td class="alignRight">#Model.Sum(Decimal.Round(item => item.ShopBalance), 2)</td>
Here is my view code:
#model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel>
#{
ViewBag.Title = "Welcome Student";
}
<h2>Welcome
#Context.User.Identity.Name
</h2>
#Html.ActionLink("[Sign Out]", "SignOut", "Student")
<ul>
<li>#Html.ActionLink("Register Courses", "registerCourses", "Course")</li>
</ul>
<%if (Model.Count == 5) { %>
<table border="1">
<tr>
<th>
RollNumber
</th>
<th>
Course Code
</th>
<th>
Course Name
</th>
<th>Add/Drop</th>
</tr>
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(0).Course.Code)
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(0).Course.Name)
</td>
<td>
#Html.ActionLink("Drop", "Drop", new { id=-1 })
</td>
</tr>
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(1).Course.Code)
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(1).Course.Name)
</td>
<td>
#Html.ActionLink("Drop", "Drop", new { id=-1 })
</td>
</tr>
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(2).Course.Code)
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(2).Course.Name)
</td>
<td>
#Html.ActionLink("Drop", "Drop", new { id=-1 })
</td>
</tr>
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(3).Course.Code)
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(3).Course.Name)
</td>
<td>
#Html.ActionLink("Drop", "Drop", new { id=-1 })
</td>
</tr>
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(4).Course.Code)
</td>
<td>
#Html.DisplayFor(modelItem => Model.ElementAt(4).Course.Name)
</td>
<td>
#Html.ActionLink("Drop", "Drop", new { id=-1 })
</td>
</tr>
</table>
<% } %>
I have added IF condition to only draw table if model count is equals to 5 but still if model contains no data then it gives error that:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
What is wrong with IF condition?
Thanks.
your code will only work if you have exactly 5 CourseRegisterModel. This is your issue.
why don't you just iterate the model(s)
#foreach(StudentRegistrationPortal.Models.CourseRegisterModel modelValue in Model)
{
<tr>
<td>
#Context.User.Identity.Name
</td>
<td>
#Html.DisplayFor(modelItem => modelValue.Course.Code)
</td>
<td>
#Html.DisplayFor(modelItem => modelValue.Course.Name)
</td>
<td>
#Html.ActionLink("Drop", "Drop", new { id=-1 })
</td>
</tr>
}
if you really insist on doing this logic inside the view, then you can use operator precedence and check for the Model containing items. Without further addo, edit you line:
<%if (Model.Count == 5) { %>
to:
// check will only continue if Model.Any() evaluates to true
#if (Model.Any() && Model.Count == 5) { ... }
I would personally do this in my viewModel inside my service or controller class and really flesh out the logic required for this hardcoded Count == 5 existing. You aslo seem to be mixing razon and webforms syntax.
Why are you using <% syntax in if clause, change it to use #
#if (Model.Count == 5)
{
also at the end change <% } %> to following
}
If Model is Null then accessing to the count would be throw an exception. so before that you have to check that if the model is null or not.
#if(Mode != null && Mode.Count == 5)
{
//....