I have been stuck on this for some time now; I'm using ASP.NET MVC 4 and C# for a web application. I read in an Excel file from my controller, and I have a List of all the cells which I send back to my view. This is what I'm using:
<table>
#foreach (var item in ViewBag.range)
{
<tr>
#for (int i = 0; i < 6; i++)
{
<td>
<input style="width:50px;" value=#item />
</td>
}
</tr>
}
</table>
Basically, I have 6 columns in Excel. I am trying to recreate the Excel in my view.
But there is something wrong with my for loop, it's doing each cell 6 times.
Can anyone help please?
It does that because you tell it to in the for loop. Perhaps you should remove it.
<table>
<tr>
#foreach (var item in ViewBag.range)
{
<td>
<input style="width:50px;" value=#item />
</td>
}
</tr>
</table>
EDIT
This will place the items inside of range into rows which have 6 columns each.
#{
int total = 0;
}
<table>
#foreach (var item in ViewBag.range)
{
if( total % 6 == 0 ){
#:<tr>
}
<td>
<input style="width:50px;" value=#item />
</td>
if( total+1 % 7 == 0 ){
#:</tr>
}
total++;
}
</table>
Since I dont have enough information about range....I did the following with assumption ; modify and use it
#{var counter=0}
<table>
<tr>
#foreach (var item in ViewBag.range)
{
counter++;
<td>
<input style="width:50px;" value=#item />
</td>
if(counter%6==0)
{
#:</tr>
if(counter<ViewBag.range.Count)
{
#:<tr>
}
}
}
</table>
Related
Hello friends I have a problem is that I want to show the return value of a field of the database in the table .. My problem is that it shows all the return values in one head .. I want only 8 numbers in each row Show me and dynamically add another line .. Thank you for your help
</div>
<div role="tabpanel" class="tab-pane fade" id="Tab3">
#if (ViewBag.ProductPartNumber != null)
{
List<ShowProductPartNumberViewModel> list = ViewBag.ProductPartNumber;
<div class="table-responsive">
<table class="table table-striped">
<tbody>
#foreach (var item in list)
{
<tr>
<td>#item.ProductNumberTitle </td>
</tr>
}
</tbody>
</table>
</div>
}
</div>
Try this:
#for (int i = 0; i < list.Count(); i += 8)
{
var items = list.Skip(i).Take(8).ToList();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
<tr>
<td>
foreach (var item in items)
{
sb.Append(item.ProductNumberTitle)
}
sb.ToString();
</td>
</tr>
}
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 have this code in C# using EF Core 1.2 where I have a form containing two submit buttons. The first button 'upload' invokes my method 'UpLoadMyFile' which compares
each line from my textarea to a pattern and returns a string which tells me if it matches one pattern.
Then I add all my text and its states into a
List <Tuple<string, string>>
that I pass to my View via a ViewModel and display each line plus its state in a table.
Now I'm trying to save each line into my database when I click my second button 'save'. But every time I try to save my lines a NullReferenceException occurs
which tells me my List from my table is null.
I would like to know how to pass all lines and states from my Table 'MyTupleList' to my Post Method since I really don't know how to fix my problem.
My View:
#model Models.ViewModels.CreateSaetzeModelView
<form asp-action="ProcessCreateLines" asp-controller="SoftwareService" method="post" enctype="multipart/form-data">
<div class="div-marginBottom">
<table class="table">
<tbody>
<tr>
<td>
<textarea name="ExpressionTextarea" id="ExpressionTextarea" runat="server" TextMode="MultiLine" asp-for="#Model.LoadExpressions"></textarea>
<div class="col-md-10">
<input type="submit" name="upload" value="upload" /> !--Calls 'uploadmyfile' action-->
</div>
</td>
<td></td>
</tr>
</tbody>
</table>
</div>
<br />
<div class="div-ExpressionEingabe">
</div>
#if (Model.MyLinesList != null)
{
<div class="div-marginBottom">
<table id="MyTupleList" class="table table_align">
<thead>
<tr>
<th>
State
</th>
<th>
MyLines
</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model.MyLinesList)
{
<tr>
<td>
#Html.DisplayFor(modelItem => item.Item2)
</td>
<td>
#Html.DisplayFor(modelItem => item.Item1)
</tr>
}
</tbody>
</table>
<input type="submit" name="save" value="save" />
</div>
}
My Code:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ProcessCreateLines(string upload, string save, CreateLinesModelView cmv)
{
//Button 'upload': Checking my lines
if (!string.IsNullOrEmpty(upload))
{
string expressions = Request.Form["ExpressionTextarea"].ToString();
List<Tuple<string, string>> result = new FileUploadController().CheckExpressions(expressions);
cmv.MyLinesList = result;
return View("ProcessCreateLines", cmv);
}
//Button 'save': Saving my lines into a Database
if (!string.IsNullOrEmpty(save))
{
// ****************************MyLinesList is null*******************
var list = cmv.MyLinesList;
...saving my list into database...
}
}
I managed to solve my problem thanks to the comment of #StephenMuecke by using instead of a Tupel a selfmade class
public class MyListModel
{
public string myLine { get; set; }
public string myState { get; set; }
}
}
and creating a List out of it in my ViewModel
public List<MyListModel> LineWithState { get; set; }
Also in my View I replaced the foreach loop with a for loop
#for (int i = 0; i < Model.LineWithState.Count; i++)
{
<tr>
<td>
#Html.TextBoxFor(m=>m.LineWithState[i].myState)
</td>
<td>
#Html.TextBoxFor(m=>m.LineWithState[i].myLine)
</tr>
}
<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>
I have some issues with a rather complex Table and Razor Tags.
I took the Most "#" out. (e.x. in front of the if etc.).
I played around for around 30 min and i can't seem to find the way to do it. I will always get error that / or similiar does not have any closing Tag. I played around with #: etc. but just can't get it.
If someone could help me out, and if someone could give a decent explanation of the #: tag, i'd highly appreciate that.
<div>
if (Model.dsInfoUser.Tables[0].Rows.Count != 0)
{
<table>
for (int i = 0; i < Model.dsInfoUser.Tables[0].Rows.Count; i++)
{
if (i % 2 == 1)
{
<tr class="tableEven">
}
else
{
<tr class="tableOdd">
}
#*Picture*#
if (i == 0)
{
<td rowspan="#Model.dsInfoUser.Tables[0].Rows.Count" class="tblPicture"><img src="#Model.dsInfoUser.Tables[0].Rows[i][1]" /></td>
}
<td>
#Model.dsInfoUser.Tables[0].Rows[i][0].ToString()
</td>
<td>
#Model.dsInfoUser.Tables[0].Rows[i][1].ToString()
</td>
</tr>
if (i == 5)
{
<tr>
<td>
<text>Member Of:</text>
</td>
<td>
<table>
for (int j = 0; j < Model.dsInfoUser.Tables[1].Rows.Count; j++)
{
if (j % 2 == 1)
{
<tr class="tableEven">
}
else
{
<tr class="tableOdd">
}
<td rowspan="3">
<div style="width: 400px; overflow-y: scroll">
</div>
</td>
</tr>
</table>
</td>
</tr>
}
</table>
}
</div>
For anyone who would like to know, here is the fixed version:
<div>
#if (Model.dsInfoUser.Tables[0].Rows.Count != 0)
{
<table>
#for (int i = 0; i < Model.dsInfoUser.Tables[0].Rows.Count; i++)
{
<tr class="#(i % 2 == 1 ? "tableEven" : "tableOdd")">
#if (i == 0)
{
<td rowspan="#Model.dsInfoUser.Tables[0].Rows.Count" class="tblPicture"><img src="#Model.dsInfoUser.Tables[0].Rows[i][1]" /></td>
}
<td>
#Model.dsInfoUser.Tables[0].Rows[i][0].ToString()
</td>
<td>
#Model.dsInfoUser.Tables[0].Rows[i][1].ToString()
</td>
</tr>
if (i == 5)
{
<tr>
<td>
<text>Member Of:</text>
</td>
<td>
<table>
#for (int j = 0; j < Model.dsInfoUser.Tables[1].Rows.Count; j++)
{
<tr class="#(i % 2 == 1 ? "tableEven" : "tableOdd")">
<td rowspan="3">
<div style="width: 400px; overflow-y: scroll">
</div>
</td>
</tr>
}
</table>
</td>
</tr>
}
}
</table>
}
</div>
You can't do it that way. Razor expects to be properly hierarchical. In particular, this is illegal:
if(condition)
{
<foo>
}
else
{
<foo>
}
</foo>
Even though we both know that would be a well-formed <foo></foo>, razor doesn't see it that way. It sees 2 unclosed <foo>, and a completely unrelated </foo> from nowhere.
In your case, the way to do this is:
<tr class="#(i % 2 == 1 ? "tableEven" : "tableOdd")">
<td>...</td>
</tr>
if (i % 2 == 1)
{
<tr class="tableEven">
}
else
{
<tr class="tableOdd">
}
is probably what is giving you trouble
you should be able to rewrite it as
string className = i%2 == 1 ? "tableEven" : "tableOdd"
<tr class="#className">
and make the parser happy