I have a table than shown one record of my model. for example list of category.
i want to create manually row number for this table.
<table class="table table-striped table-bordered table-hover table-full-width dataTable">
#foreach (var item in Model)
{
<tr>
<td>
**I want to Show Row Number Here**
</td>
<td>
#Html.ActionLink(item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.Id }, null)
</td>
</tr>
}
</table>
Define a counter rowNo and write
#{ int rowNo = 0; }
#foreach (var item in Model) {
<tr style="background: #classtr;" >
<td>
#(rowNo += 1)
</td>
Define a counter rowNo and write
<table class="table table-striped table-bordered table-hover table-full-width dataTable">
#{int rowNo;}
#foreach (var item in Model)
{
<tr>
<td>
#rowNo++;
</td>
<td>
#Html.ActionLink(item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.Id }, null)
</td>
</tr>
}
</table>
None of the above worked for me - I did this:
#{int RowNo = 0;}
#foreach (var item in Model) {
<tr>
<td>#{RowNo++;} #RowNo</td>
You may use the Select overload:
<table class="table table-striped table-bordered table-hover table-full-width dataTable">
#foreach (var item in Model.Select((item,index)=>new{item,index})
{
<tr>
<td>
#item.index
</td>
<td>
#Html.ActionLink(item.item.Name, "ViewSubCategory", "ProductSubCategory", new { id = item.item.Id }, null)
</td>
</tr>
}
</table>
I've found the next option for my ASP.Net MVC project. All above options didn't work for my project.
#foreach (var item in Model.Select((x, i) => new { Data = x, Index = i + 1 }))
{
<tr>
<td>
#item.Index
</td>
<td>
#Html.DisplayFor(modelItem => item.Data.ReviewerName)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data.ReviewerComments)
</td>
<td>
#Html.DisplayFor(modelItem => item.Data.ReviewerRating)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.Data.Id })
</td>
</tr>
}
I think this code will solve the problem
let rows = document.querySelectorAll('tr');
rows.forEach((row) => {
let z = document.createElement("td");
z.textContent = `(row #${row.rowIndex})`;
row.appendChild(z);
});
<table>
<thead>
<tr><th>Item</th> <th>Price</th></tr>
</thead>
<tbody>
<tr><td>Bananas</td> <td>$2</td></tr>
<tr><td>Oranges</td> <td>$8</td></tr>
<tr><td>Top Sirloin</td> <td>$20</td></tr>
</tbody>
<tfoot>
<tr><td>Total</td> <td>$30</td></tr>
</tfoot>
</table>
Easiest approach is to use CSS to have the row numbers in your tables in the index of you MVC View.
.css-serial {
counter-reset: serial-number;
/* Set the serial number counter to 0 */
}
.css-serial td:first-child:before {
counter-increment: serial-number+1;
/* Increment the serial number counter */
content: counter(serial-number);
/* Display the counter */
}
table{
font-family: Arial;
border:solid;
border-width:1px;
border-color:#dedede;
border-radius:5px;
}
th{
background-color:#efefef;
}
td{
padding:3px;
margin:3px;
}
<table class="css-serial">
<tr>
<th>Row #</th>
<! –– Row Number goes here automatically with CSS style ––>
<th>Name</th>
</tr>
<tr>
<td></td>
<td>John</td>
</tr>
<tr>
<td></td>
<td>Richard</td>
</tr>
<tr>
<td></td>
<td>Joe</td>
</tr>
<tr>
<td></td>
<td>Rogan</td>
</tr>
<tr>
<td></td>
<td>Lebron</td>
</tr>
<tr>
<td></td>
<td>Marcus</td>
</tr>
<tr>
<td></td>
<td>Jane</td>
</tr>
<tr>
<td></td>
<td>Maria</td>
</tr>
</table>
Related
I want to add one more field to my view. So basically, after displaying a town in the dropdown menu, I want to display the (*ABR) field as seen here.
As you can see from the picture, after Advance Ortopedical, I just want to add a filed called *ABR.
<table class="table datatable-responsive datatable-medical-map" id="medProviders" style="width:100%">
<thead>
<tr class="bg-info">
<th>Medical Provider (* Choose one)</th>
<th>Distance (miles)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
#{
int i = 0;
foreach (var item in medProviders)
{
<tr class="sortList" style="cursor:pointer" id="increment-#i" data-id="#item.Id" data-lat="#item.Latitude" data-long="#item.Longitude">
<td>#item.Firstname</td>
<td id="distance-#i"></td>
<td id="duration-#i"></td>
</tr>
i++;
}
}
</tbody>
</table>
<p id="medicalValidation"></p>
Any suggestions or comments on how to do this in a simple way?
Please use this code. This will add 1 more field at the end of table.
#{
int i = 0;
foreach (var item in medProviders)
{
<tr class="sortList" style="cursor:pointer" id="increment-#i" data-id="#item.Id" data-lat="#item.Latitude" data-long="#item.Longitude">
<td>
#item.Firstname
<br>*ABR</br>
</td>
<td id="distance-#i"></td>
<td id="duration-#i"></td>
</tr>
i++;
}
}
</tbody>
I implemented a responsive dataTable for mobile (iPhone), it is showing all the columns except for the last column with the CRUD action links, like Edit, Details, and Delete.
Mobile View
Desktop View
The dataTable is also not showing the plus icon to expand & close certains columns. What am I missing in creating the dataTable?
<div class="table-responsive">
<table id="dataTable" class="table table-striped table-bordered dt-responsive nowrap" cellspacing="0" style="width: 100%;">
<thead>
<tr>
<th>
#Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th>
First Name
</th>
<th>
#Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.LastName)
</td>
<td>
#Html.DisplayFor(modelItem => item.FirstMidName)
</td>
<td>
#Html.DisplayFor(modelItem => item.EnrollmentDate)
</td>
<td>
#Html.ActionLink("Details", "Details", new { id = item.ID })
#if (User.IsInRole("Admin"))
{
#:|
#Html.ActionLink("Edit", "Edit", new { id = item.ID })#: |
#Html.ActionLink("Delete", "Delete", new { id = item.ID })
}
else
{
}
</td>
</tr>
}
</tbody>
</table>
</div>
I was missing a JavaScript tag for responsive dataTable and the number of children of my <tr> element didn't match the number of <th> elements that were a child of the <thead> element."
I would like to take the model as in view?
#model Dictionary<string,List<string>>
<table width="60%">
<tr class="ui-widget-header">
<td>
___Category___
</td>
<td>
___Detail___
</td>
</tr>
<tr>
<td>
#Model.Key
</td>
<td>
#Model.Count
</td>
</tr>
</table>
How could I do this?
here is my view with razor syntax.
Please help me.
Yes you can do it. Below is the sample
#model Dictionary<string,List<string>>
#foreach (KeyValuePair<string, List<string>> pair in Model)
{
<label> #pair.Key</label>
<div>
#foreach(string data in #pair.Value)
{
<span>#data </span>
}
</div>
}
Add #model Dictionary<string,List<string>> to your view (assuming Razor is used).
#model Dictionary<string, List<string>>
#foreach (var group in Model)
{
<h2>#group.Key</h2>
<table>
<tr>
<th>Title</th>
</tr>
#foreach (var aString in group.Value){
<tr>
<td>#aString </td>
</tr>
}
</table>
}
I have the following code and I want to put the table headers outside of the #foreach but when i move it outside It no longer sees the closing tag. Thanks
#foreach (var item in Model)
{
<div class="data" ><label for="fullName" >Name: </label>#item.UserName.Replace('.', '')</div>
if (Model.Count > 0)
{
<table class="data">
<tr>
<th>
Work Date
</th>
<th>
Change Details
</th>
<th>
Ip Address
</th>
<th></th>
</tr>
<tr>
<td>
#{
var stringDate = item.WorkDate.ToShortDateString();
}
#Html.DisplayFor(modelItem => stringDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ChangeDetail)
</td>
<td>
#Html.DisplayFor(modelItem => item.IpAddress)
</td>
</tr>
</table>
}
<br />
<hr />
}
So this is kinda what I'm trying to accomplish, but it keeps giving me errors no matter what way I try it.
<table class="data">
<tr>
<th>
Work Date
</th>
<th>
Change Details
</th>
<th>
Ip Address
</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<div class="data" ><label for="fullName" >Name: </label>#item.UserName.Replace('.','')</div>
if (Model.Count > 0)
{
<tr>
<td>
#{
var stringDate = item.WorkDate.ToShortDateString();
}
#Html.DisplayFor(modelItem => stringDate)
</td>
<td>
#Html.DisplayFor(modelItem => item.ChangeDetail)
</td>
<td>
#Html.DisplayFor(modelItem => item.IpAddress)
</td>
</tr>
</table>
}
<br />
<hr />
}
You must have your table closing tag outside of the for each loop.
Short answer: the closing tag </table> has to be placed outside of the foreach loop.
Long answer: You should put the Model.Count > 0 outside of the foreach loop because the moment you enter the code block within the loop, Model.Count > 0 always evaluates to true because you have at least one item.
If – and only if – your model contains any items, you print out the table header. After that, you append one row for each item in Model. Both <table> and </table> have to occur within the same level of nesting.
#if (Model.Count > 0)
{
<table class="data">
<tr>
<th>Date</th>
<th>Change Details</th>
<th>Ip Address</th>
<th></th>
</tr>
#foreach (var item in Model)
{
<div class="data" >
<label for="fullName" >Name: </label>
#item.UserName.Replace('.', ' ')
</div>
#{
var stringDate = item.WorkDate.ToShortDateString();
}
<tr>
<td>#Html.DisplayFor(modelItem => stringDate)</td>
<td>#Html.DisplayFor(modelItem => item.ChangeDetail)</td>
<td>#Html.DisplayFor(modelItem => item.IpAddress)</td>
</tr>
}
</table>
<br />
<hr />
}
You should consider moving the logic (#item.UserName.Replace('.', ' ')) out of the view into the corresponding controller action.
It does not seem to like #{index++;} , I have tried
#{int index++}, #(index++), #(int index++;)
This code didn't throw errors when used with MVC 2.
Here's it's giving me
ambiguity warnings about index.
#model CartTest.Models.Cart
#{
ViewBag.Title = "Index";
}
<h2>Cart Index</h2>
<table width="80%" align="center">
<thead><tr>
<th align="center">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr></thead>
<tbody>
#{int index = 0;}
#foreach (var line in Model.Lines)
{
<tr>
#Html.Hidden("Lines.Index", index);
<td align="center">#Html.TextBox("Lines[" + index + "].Quantity",line.Quantity)</td>
<td align="left">#line.Product.Name</td>
<td align="right">#line.Product.Price</td>
<td align="right">#(line.Quantity * line.Product.Price)</td>
<td align="right">#Html.ActionLink("Remove", "RemoveItem", new { productId = line.Product.ProductID }, null)</td>
</tr>
#{index++;}
}
</tbody>
<tfoot>
</tfoot>
</table>
Try like this:
#{int index = 0;}
#foreach (var line in Model.Lines)
{
<tr>
...
</tr>
index++;
}
Now that's just to make the Razor compiler happy. It's not a solution I recommend. The real solution I would recommend you is to use editor templates:
<table width="80%" align="center">
<thead>
<tr>
<th align="center">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr>
</thead>
<tbody>
#Html.EditorFor(x => x.Lines)
</tbody>
<tfoot>
</tfoot>
</table>
and inside the corresponding editor template of a Line (~/Views/Shared/EditorTemplates/LineViewModel.cshtml) which will be rendered for each element of the Line collection:
#model LineViewModel
<td align="center">
#Html.TextBoxFor(x => x.Quantity)
</td>
<td align="left">
#Html.DisplayFor(x => x.Product.Name)
</td>
<td align="right">
#Html.DisplayFor(x => x.Product.Price)
</td>
<td align="right">
#Html.DisplayFor(x => x.CalculatedTotalPrice)
</td>
<td align="right">
#Html.ActionLink("Remove", "RemoveItem", new { productId = Model.Product.ProductID }, null)
</td>
See, no more ugly loops, weakly typed helpers, dealing with some indexes, etc... Everything works by conventions.