MVC formatting td dynamically - asp.net-mvc

I am new to razor engine syntax and I have issue with formatting adding the logic in the view. Here is what I want to accomplish. I have collection of items coming from model.
I need to iterate that collection and align 6 item in a row.
This is how the final output should look like:
<table>
<tr>
<td>checkbox comes here</td>
<td>checkbox comes here</td>
<td>checkbox comes here</td>
</tr>
<tr>
<td>checkbox comes here</td>
<td>checkbox comes here</td>
<td>checkbox comes here</td>
</tr>
................. and so on
</table>
Here is the code I wrote in the view
<table>
#for (int i = 0; i <= Model.checkItems.Count; i++)
{
if (i % 6 == 0)
{ <tr> }
<td>
<input type="checkbox"
id="chk_#(Model.checkItems[i].DisplayText)"
name="chk"
nameVal = "#Model.checkItems[i].DisplayText"
value="#Model.checkItems[i].Value"/>
<label for="chkGroup_#(Model.checkItems[i].DisplayText)">#Model.checkItems[i].DisplayText
</td>
if(i % 6 == 0)
{ </tr> }
}
</table>
When the page finally gets rendered, the first if condition getting executed but not the second one.
Can somebody help to figure out how the accomplish this?

Try this
<table>
#{ int count = 0; }
<tr>
#foreach (var item in Model.checkItems)
{
<td>
<input type="checkbox"
id="chk_#(item.DisplayText)"
name="chk"
nameVal = "#item.DisplayText"
value="#item.Value"/>
<label for="chkGroup_#(item.DisplayText)">#item.DisplayText</label>
</td>
if (++count % 6 == 0)
{
#:</tr><tr>
}
}
</tr>
</table>

You're missing a closing tag on your label
<label for="chkGroup_#(Model.checkItems[i].DisplayText)">#Model.checkItems[i].DisplayText
should be
<label for="chkGroup_#(Model.checkItems[i].DisplayText)">#Model.checkItems[i].DisplayText</label>

would this work:
<table>
#for (int i = 0; i <= Model.checkItems.Count; i++)
{
if (i % 6 == 0)
{
<tr>
<td>
<input type="checkbox"
id="chk_#(Model.checkItems[i].DisplayText)"
name="chk"
nameVal = "#Model.checkItems[i].DisplayText"
value="#Model.checkItems[i].Value"/>
<label for="chkGroup_#(Model.checkItems[i].DisplayText)">#Model.checkItems[i].DisplayText
</td>
</tr>
}
}
</table>

Here's another solution that uses 2 nested loops for rows and columns. I don't know if its necessarily better (it certainly looks more complicated on the face of it), but it does at least allow you to easily see the nested nature of the rows and cells:
<table>
#{
const int cols = 3;
int rows = (Model.checkItems.Count + cols - 1) / cols;
}
#for (int rowIndex = 0; rowIndex < rows; rowIndex++)
{
<tr>
#for (int colIndex = rowIndex * cols; colIndex < Math.Min(Model.checkItems.Count, cols * (rowIndex + 1)); colIndex++)
{
<td>
<input type="checkbox"
id="chk_#(Model.checkItems[colIndex].DisplayText)"
name="chk"
nameVal="#Model.checkItems[colIndex].DisplayText"
value="#Model.checkItems[colIndex].Value"/>
<label for="chkGroup_#(Model.checkItems[colIndex].DisplayText)">#Model.checkItems[colIndex].DisplayText</label>
</td>
}
</tr>
}
</table>

Related

How do I change Table view of products into Side By Side View, like online shopping products list with HTML5 and CSS3 in MVC Razor page?

I am trying to create a list of movies where the data is coming from the database table. So I need to create the Side By Side view of products using #foreach(){ .... }. Please someone review my code below and help me get through this step.
I want my products as (using #foreach item):
Product 1 Product 2 Product 3
Product 4 Product 5 Product 6
I tried to do
#foreach(var i=0; i< Model.Count; i+=2)
{
}
but didn't work maybe because of my images, which are coming from db as Imagepath.
<table class="table">
<tr>
<th>
#Html.DisplayNameFor(model => model.MoviesName)
</th>
<th>
#Html.DisplayNameFor(model => model.ImgPath)
</th>
<th>
#Html.DisplayNameFor(model => model.ReYear)
</th>
<th>
#Html.DisplayNameFor(model => model.UnitPrice)
</th>
</tr>
#foreach (var item in Model)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.MoviesName)
</td>
<td>
<img src="#Url.Content("~/Image/" +
System.IO.Path.GetFileName(item.ImgPath))" alt=""
width="75" , height="100" />
</td>
<td>
#Html.DisplayFor(modelItem => item.ReYear)
</td>
<td>
#String.Format("{0:c}", item.UnitPrice)
</td>
<td>
#Html.ActionLink("Edit", "Edit", new { id = item.MovieID
}) |
#Html.ActionLink("Details", "Details", new { id =
item.MovieID
}) |
#Html.ActionLink("Delete", "Delete", new { id =
item.MovieID
})
</td>
</tr>
}
</table>
To build a side-by-side layout, you'll need to choose either a <div> based or a <table> based layout.
<div> based is easiest as you can use a #foreach loop to build a <div>-based, side-by-side layout easily. For example, here's a super simple way of doing it using Bootstrap 4:
<div class="row">
#foreach (var item in Model)
{
<div class="col-4">...Build your product in here...</div>
}
</div>
Or...you can make your layout using a <table>, but this is more complex; you'll need to use nested loops and a bit of math to get the rows and columns right. Here's one way to achieve this:
First, figure out how many rows there'll be based on Model.Count products:
#{
int columns = 3;
int rows = (int)Math.Ceiling(Model.Count / (float)columns);
}
Next, setup nested loops similar to this:
<table>
#for (int row = 0; row < rows; ++row)
{
<tr>
#for (int column = 1; column <= columns; ++column)
{
#if ((row * columns) + column <= Model.Count)
{
<td>...Build your product in here...</td>
}
else
{
break;
}
}
</tr>
}
</table>

Adding field to ASP.NET MVC view

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>

ASP.NET razor engine seems retard [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a view that contains nothing but the following:
<table>
#for (int i = 0; i < 10; i++)
{
if (i % 5 == 0)
{
<tr>
}
<td>
Hello
</td>
if (i % 5 == 0)
{
</tr>
}
}
</table>
I intended to produce is the following
<table>
<tr>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
<tr>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
<td>Hello</td>
</tr>
</table>
But instead it produced the following:
<table>
<tr>
}
<td>
Hello
</td>
if (i % 5 == 0)
{
</tr>
<tr>
}
<td>
Hello
</td>
if (i % 5 == 0)
{
</tr>
</table>
What is wrong?
You have to prefix any HTML you wish to show on the page with #: when it is within a code block
<table>
#for (int i = 0; i < 10; i++)
{
if (i % 5 == 0)
{
#:<tr>
}
#:<td>
#:Hello
#:</td>
if (i % 5 == 0)
{
#:</tr>
}
}
</table>
#for (int i = 0; i < 10; i++)
{
if (i % 5 == 0)
{
#:<tr>
}
<td>
Hello
</td>
if (i % 5 == 0)
{
#:</tr>
}
}
</table>
To produce the desired output you actually want the following:
<table>
#for (int i = 0; i < 10; i++)
{
if (i % 5 == 0)
{
#:<tr>
}
#:<td>
#:Hello
#:</td>
if ((i + 1) % 5 == 0)
{
#:</tr>
}
}
</table>
Notice the change to the second if statement. Alternatively you could use nested for loops to produce the same result.
<table>
#for (int i = 0; i < 2; i++)
{
<tr>
#for (int j = 0; j < 5; j++)
{
<td>Hello</td>
}
</tr>
}
</table>
You're code block should is across multiple lines so should be within braces (braces after the #)
#{
for (int i = 0; i < 10; i++)
{
Blah blah...
}
}

Trouble writing HTML in Razor view if/else condition

I'm not quite sure what I am missing here, but I am having trouble adding a class='success' to a <tr> in a Partial View while iterating through a list in my Model.
My goal is to have the first row's tr to have the attribute class='success'
Here is the code I have:
#model List<DxRow>
<div class="row">
<table class="table" id="diagnosis">
<tr id="header-row">
<th>Dx Code</th>
<th>Dx Description</th>
<th>Dx Date</th>
<th>OnSet Or Ex</th>
<th>Dx Order (Top is Primary)</th>
</tr>
#{ bool IsFirst = true; }
#foreach (DxRow r in Model)
{
if (IsFirst)
{
Html.Raw("<tr class='success'>");
IsFirst = false;
}
else
{
Html.Raw("<tr>");
}
<tr>
<td>#r.dxCode</td>
<td>#r.dxDescription</td>
<td>#r.dxDate</td>
<td>#r.dxType</td>
<td>
<span class='up'><i class='icon-arrow-up bigger-160' style='color:green'></i></span>
<span class='down'><i class='icon-arrow-down bigger-160' style='color:red'></i></span>
<span class='top'><i class='icon-star bigger-160' style='color:goldenrod'></i></span>
<span class='delete'><i class='icon-trash bigger-160' style='color:red'></i></span>
</td>
</tr>
}
</table>
When I step through the code, it goes into the special IsFirst section the first iteration and the else section on all other iterations.
But, when I use FireBug to view the source code, it is not writing the part: <tr class='success'> , just the <tr>.
I think the extra <tr> after your if statement might be causing the browser to ignore your html, as the html would actually be broken with two <tr> tags being rendered.
I would probably update the code to be more like;
#{ bool IsFirst = true; }
#foreach (DxRow r in Model)
{
<tr class="#( IsFirst ? "success" : "")">
<td>#r.dxCode</td>
<td>#r.dxDescription</td>
<td>#r.dxDate</td>
<td>#r.dxType</td>
<td>
<span class='up'><i class='icon-arrow-up bigger-160' style='color:green'></i></span>
<span class='down'><i class='icon-arrow-down bigger-160' style='color:red'></i></span>
<span class='top'><i class='icon-star bigger-160' style='color:goldenrod'></i></span>
<span class='delete'><i class='icon-trash bigger-160' style='color:red'></i></span>
</td>
</tr>
IsFirst = false;
}
Since you are setting IsFalse to false after the first loop. Makes the code tidier.

Create N*M <table> in view by iterating through model items

Well, easily I have a model:
#model IEnumerable<CompanyWebSite.Category>
And it's quite straightforward to iterating through its items and create a One-item-per-row table to show them.
And the problem is that I want to create a table like this:
<table>
<tr>
<td></td>
<td></td>
</tr>
//
// ...
//
<tr>
<td colspan="2"> ... LastItem ...</td>
</tr>
</table>
Seems simple, but I can't do that...!
#{
ViewBag.Title = "Products";
var last = Model.Last();
int i = 1;
}
<table id="tblCats">
#foreach(var item in Model)
{
if (i == 1){
<tr>
#(i = 2)
}
#if (!item.Equals(last))
{
<td>
<h4>#item.CategoryName</h4>
<label>#item.Description</label>
</td>
if (i == 2)
{
</tr>
i = 1;
}
else
i = 2;
}
else
{
<td colspan="2">
</td>
</tr>
}
}
Because of the first opening <tr>, Razor gets confused and doesn't see the closing }s and else statements... What can I do...?!
You could use HtmlHelper.Raw method to help razor

Resources