<div>
<table >
<tr>
<th>Customer ID</th>
<th>Name</th>
<th>Type</th>
</tr>
#foreach (var a in Model.Attachments)
{
<tr>
<td>
#a.CId
</td>
<td>
#a.CName
</td>
<td>
#a.CType
</td>
</tr>
}
</table>
#Html.PagedListPager((IPagedList)Model.Attachments, page => Url.Action("Index", new { page }))
</div>
Currently I am displaying 25 items per page. If the final page does not have 25 items, I want to append rows to the end of the table, to keep the Page selector at the same level from page to page.
This seems like it would work, I just don't know where to put it:
#for (int i = 25; i > Model.Attachments.Count() % 25; i--)
{
<tr>
<td></td>
</tr>
}
You have for sure a loop thrown the attachments list.
Put this loop right after the loop where you write your TRs.
Just take into account that if your data makes your TRs higher, this will break your solution. Other thing you may try is adding a HTML space in your dummy rows:
<div>
<table >
<tr>
<th>Customer ID</th>
<th>Name</th>
<th>Type</th>
</tr>
#foreach (var a in Model.Attachments)
{
<tr>
<td>
#a.CId
</td>
<td>
#a.CName
</td>
<td>
#a.CType
</td>
</tr>
}
#for (int i = 25; i > Model.Attachments.Count() % 25; i--)
{
<tr>
<td></td>
</tr>
}
</table>
#Html.PagedListPager((IPagedList)Model.Attachments, page => Url.Action("Index", new { page }))
</div>
Related
I have a table getting data from a model with a foreach, in a razor view.. I want to add a new row every 10 rows, how can I do that?
EDIT
<tbody>
#foreach (var context in sortedData)
{
#for (int i = 1; i % 10 == 0;i++)
{
<tr>
<td>
new row
</td>
</tr>
}
<tr>
<td>
#context.Id
</td>
<td>#context.CantidadElegida</td>
<td>#context.Item</td>
<td>#String.Format("RD${0:f2}", #context.Price)</td>
<td>#String.Format("RD${0:f2}", #context.Reposition)</td>
<td>#String.Format("RD${0:f2}", #context.SubTotal)</td>
</tr>
}
</tbody>
Seems you are looking for this.
int count=1;
#foreach (var context in sortedData)
{
if ( count % 10 == 0)
{
<tr>
<td>
new row
</td>
</tr>
}
<tr>
<td>
#context.Id
</td>
<td>#context.CantidadElegida</td>
<td>#context.Item</td>
<td>#String.Format("RD${0:f2}", #context.Price)</td>
<td>#String.Format("RD${0:f2}", #context.Reposition)</td>
<td>#String.Format("RD${0:f2}", #context.SubTotal)</td>
</tr>
count++;
}
What about this:
int count=1;
#foreach (var context in sortedData)
{
if ( count++ % 10 == 0)
{
<tr>
<td>
new row
</td>
</tr>
}
<tr>
<td>
#context.Id
</td>
<td>#context.CantidadElegida</td>
<td>#context.Item</td>
<td>#String.Format("RD${0:f2}", #context.Price)</td>
<td>#String.Format("RD${0:f2}", #context.Reposition)</td>
<td>#String.Format("RD${0:f2}", #context.SubTotal)</td>
</tr>
}
I'm trying to display various information with the help of a for each loop.
I've created a for each loop, that iterates through a list of item. In this list there is another list of items.
As for now it's displayed like this:
My code is:
#foreach (Invoice invoice in ViewBag.Invoices)
{
<table>
<tr>
<th>Customer</th>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Price</th>
</tr>
#foreach (OrderItem orderItem in invoice.OrderItems)
{
<tr>
<td>#invoice.Customer.Firstname #invoice.Customer.Lastname</td>
<td>#orderItem.Product.Title </td>
<td>#orderItem.Quantity </td>
<td>#orderItem.Product.Price </td>
<td>#orderItem.TotalPrice</td>
</tr>
}
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td class="border-top">#invoice.TotalPrice</td>
</tr>
</table>
}
However, I don't want it to display the customers name twice. And I don't want to have the customer on its own row. Therefore I tried to put the starting tag along with the customer outside of the foreach loop, and instead ending the foreach loop with a <tr> tag. So it'd look like this:
#foreach (Invoice invoice in ViewBag.Invoices)
{
<table>
<tr>
<th>Customer</th>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Price</th>
</tr>
<tr>
<td>#invoice.Customer.Firstname #invoice.Customer.Lastname</td>
#foreach (OrderItem orderItem in invoice.OrderItems)
{
<td>#orderItem.Product.Title </td>
<td>#orderItem.Quantity </td>
<td>#orderItem.Product.Price </td>
<td>#orderItem.TotalPrice</td>
</tr>
<tr>
}
<td></td>
<td></td>
<td></td>
<td></td>
<td class="border-top">#invoice.TotalPrice</td>
</tr>
</table>
}
UPDATED SOLUTION
I followed the wise words of #Ed Plunkett and initialized a local string to null. The I created an if/else statement to check if the previous customer has been set, and initialized the value of the previous customer in the end of the loop.
#{string prevCust = null; }
#foreach (Invoice invoice in ViewBag.Invoices)
{
<table>
<tr>
<th>Customer</th>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Price</th>
</tr>
#foreach (OrderItem orderItem in invoice.OrderItems)
{
<tr>
#if (prevCust != invoice.Customer.Firstname + invoice.Customer.Lastname)
{
<td>#invoice.Customer.Firstname #invoice.Customer.Lastname</td>
}
else
{
<td></td>
}
<td>#orderItem.Product.Title </td>
<td>#orderItem.Quantity </td>
<td>#orderItem.Product.Price </td>
<td>#orderItem.TotalPrice</td>
</tr>
prevCust = invoice.Customer.Firstname + invoice.Customer.Lastname;
}
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td class="border-top">#invoice.TotalPrice</td>
</tr>
</table>
}
I'd do it caveman style: Just make a local variable String prevCustomerName, initialize to null, and update it at the end of the loop block. At the beginning of the loop block, if the new customer name is the same as prevCustomerName, don't insert it into the HTML for that row.
I enjoyed your original code with </tr>\n<tr> in the inner loop, but it took me a minute to figure out what you were doing, and Razor seems to be totally baffled. And you still need to ugly it up with a special case to add an empty cell on non-first rows. If you're stuck putting in an if either way, do the caveman.
When resorting to iteration in a table, it's helpful (even for yourself, not just for razor or whatever view engine you are using) to keep it as structured and simple as possible.
(the comments on your question should be enough explanation as to why this is a good idea)
try using multiple tr's per invoice to achieve your goals. for example :
#foreach (Invoice invoice in ViewBag.Invoices)
{
<table>
<tr>
<th>Customer</th>
<th>Product</th>
<th>Quantity</th>
<th>Price</th>
<th>Total Price</th>
</tr>
<tr>
<td colspan="4">#invoice.Customer.Firstname #invoice.Customer.Lastname</td>
</tr>
#foreach (OrderItem orderItem in invoice.OrderItems)
{
<tr>
<td>#orderItem.Product.Title </td>
<td>#orderItem.Quantity </td>
<td>#orderItem.Product.Price </td>
<td>#orderItem.TotalPrice </td>
</tr>
}
<tr>
<td colspan="4"></td>
<td class="border-top">#invoice.TotalPrice</td>
</tr>
</table>
}
You could even resort to generating one table per invoice if that helps make things simpler for you.
Without being ASP-expert I suppose you have to handle the first row differently then the others :
<tr>
<td>#invoice.Customer.Firstname #invoice.Customer.Lastname</td>
<td>#orderItem.Product.Title </td>
<td>#orderItem.Quantity </td>
<td>#orderItem.Product.Price </td>
<td>#orderItem.TotalPrice </td>
</tr>
Now handle the rest in the loop omitting the first (already handled) element:
#foreach (OrderItem orderItem in invoice.OrderItems.Skip(1))
{
<tr>
<td/>
<td>#orderItem.Product.Title </td>
<td>#orderItem.Quantity </td>
<td>#orderItem.Product.Price </td>
<td>#orderItem.TotalPrice </td>
</tr>
}
A more generic approach would be using a for-loop with an index and check if you´re handling the first element:
#for(int i = 0; invoice.OrderItems.Count; i++)
{
<tr>
<td>#(i != 0 ? invoice.Customer.Firstname + " " + invoice.Customer.Lastname : "")</td>
<td>#invoice.OrderItems[i].Product.Title </td>
<td>#invoice.OrderItems[i].Quantity </td>
<td>#invoice.OrderItems[i].Product.Price </td>
<td>#invoice.OrderItems[i].TotalPrice </td>
</tr>
}
However I´m not sure if this is even valid ASP-syntax.
I have a razor view that loses the variable scope once inside the forloop. It works fine until I added the table tags. So all my calls to the different parts of the ViewModel no longer work. Please let me know if there is anything else that would help to know.
#model Auditor_Evaluations_MVC.ViewModels.EvaluationViewModel
#using (Html.BeginForm())
{
<div>
<table class="table table-condensed" style="width:825px;">
for(int i = 0; i < Model.QuestionGroup.Count; i++)
{
var questionGroup = Model.QuestionGroup[i];
#Html.HiddenFor(m => Model.QuestionGroup[i].GroupName)
#Html.HiddenFor(m => Model.QuestionGroup[i].GroupId)
<thead>
<tr>
<th class="text-left">#Html.Label(questionGroup.GroupName) </th>
<th class="text-center">Excellent</th>
<th class="text-center">Good</th>
<th class="text-center">Fair</th>
<th class="text-center">Poor</th>
<th class="text-center">N/A</th>
</tr>
</thead>
var questions = questionGroup.Questions;
for(int j = 0; j < questions.Count; j++)
{
#Html.HiddenFor(m => Model.QuestionGroup[i].Questions[j].QuestionName)
<tr>
<td class="text-left">#Html.Label(questions[j].QuestionName) </td>
var questionResponse = questions[j].QuestionResponses;
#for(int k = 0; k < questionResponse.Count; k++)
{
<td class="text-center" style="width:75px">
#Html.RadioButtonFor(m => Model.QuestionGroup[i].Questions[j].SelectedAnswer,questionResponse[k].QuestionValue)
</td>
}
</tr>
}
}
<tr>
<td colspan="6"> </td>
</tr>
<tr>
<td colspan="6"><strong>#Model.GeneralQuestion1Text</strong></td>
</tr>
<tr>
<td colspan="6">#Html.TextAreaFor(m => m.GeneralQuestion1Value)</td>
</tr>
<tr>
<td colspan="6"><strong>2. Additional Comments:</strong></td>
</tr>
<tr>
<td colspan="6">#Html.TextAreaFor(m => m.GeneralQuestion2Value)</td>
</tr>
<tr>
<td colspan="6"><strong>3. Would you like the appropriate audit manager to contact you regarding any or all of the information relayed through this questionnaire? If yes, please include your name and telephone number.</strong></td>
</tr>
<tr>
<td colspan="6">#Html.TextAreaFor(m => m.GeneralQuestion3Value)</td>
</tr>
<tr>
<td colspan="6"><input type="submit" value="Submit"/></td>
</tr>
</table>
</div>
}
I'm not 100% sure on exactly what your problem is, the question is a little confusiing, but all the places where you do something like this:
#Html.HiddenFor(m => Model.QuestionGroup[i].GroupName)
Should probably be replaced with
#Html.HiddenFor(m => m.QuestionGroup[i].GroupName)
i.e. use the m variable to represent your model in the lambda expression, rather than referencing the Model property.
Also, on this line:
<table class="table table-condensed" style="width:825px;">
for(int i = 0; i < Model.QuestionGroup.Count; i++)
You need to add an # in front of the for keyword.
Try using a foreach in that Razor block
foreach(var question in Model.QuestionGroup) {
#Html.HiddenFor(m => m.question.GroupName)
#Html.HiddenFor(m => m.question.GroupId)
...
etc.
update: you may need to look into the EditorFor and EditorForModel HTML helpers if your model is not mapping to the correct ViewModel when send it
Enum:
public enum Enumenter
{
Products = 10,
Users = 20,
Actions = 30
}
my view:
#model IEnumerable<ServerDB.Tables.EntityType>
#{
ViewBag.Title = "Infrastruktur";
}
<table class="table">
<tr>
<th>
<h2>Infrastruktur</h2>
</th>
<th></th>
</tr>
#foreach (var val in Enum.GetNames(typeof(ServerDB.Tables.Enum)))
{
<tr>
<td>
#Html.DisplayName((String.Format("{0}{1}", val, "2")))
</td>
<td></td>
<td>
<p>
</p>
</td>
<td></td>
</tr>
}
#foreach (var Ost in Enum.(typeof(ServerDB.Tables.Enum)))
{
#Html.DisplayName((String.Format("{0}", Ost)))
}
</table>
How do i Print out The values next to the products?
im going to use the numbers from the enum to make a query search, so i can search for any product, any users or actions for that matter.
im no expert in this, so please be gentle.
Enum.GetValues returns values for the enum as objects, so you need to cast them to int value (using int instead of var will do that for you). After that you can use Enum.GetName to get the name for specific value:
#foreach (int val in Enum.Values(typeof(ServerDB.Tables.Enum)))
{
var name = Enum.GetName(typeof(ServerDB.Tables.Enum), val);
<tr>
<td>
#Html.DisplayName((String.Format("{0}{1}", name, "2")))
</td>
<td>
#val
</td>
<td>
<p>
</p>
</td>
<td>
</td>
</tr>
}
There is another approach which doesn't use the Enum.GetName method. You simply cast the value you get from Enum.Values to string to get the name and to int to get the value
#foreach (var name in Enum.Values(typeof(ServerDB.Tables.Enum)))
{
var val = (int)name;
<tr>
<td>
#Html.DisplayName((String.Format("{0}{1}", name, "2")))
</td>
<td>
#val
</td>
<td>
<p>
</p>
</td>
<td>
</td>
</tr>
}
Below is my razor code for my table of users and the current add/edit layout
<table class="smalltable" cellpadding="0" cellspacing="0" id="tblUsers">
<thead>
<tr>
<th> </th>
<th>First Name</th>
<th>Last Name</th>
<th>Dept</th>
<th>Assyst Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>#{ Html.BeginForm("AddEditUser", "Home"); }
<input type="submit" value="Add" id="btnSave" name="cmd" class="plain-button" />
</td>
<td>#Html.Editor("Forename")</td>
<td>#Html.Editor("Surname")</td>
<td>#Html.DropDownList("lstDepts")</td>
<td>#Html.Editor("AssystName")
#{ Html.EndForm(); }</td>
</tr>
#foreach (var User in Model)
{
<tr>
<td>#Html.ActionLink("Edit", "AddEditUser", new { id = User.ID }, new { name = "cmd", value = "Edit" })</td>
<td>
#User.Forename
</td>
<td>
#User.Surname
</td>
<td>
#User.Dept
</td>
<td>
#User.AssystName
</td>
</tr>
}
</tbody>
</table>
What i want to achieve is when a edit is pressed, the row that the edit link was on turns into edit boxes, i how to load up the add form with the edit users details (i could use hidden id and set it with querystring) but i wanted to edit in place really
anyone know how to do this or can point me in the right place?
THanks