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
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 make a calendar table which uses a condition to jump to next row everytime (DayOfTheWeek = Sunday), but i cant use <tr> because Google Chrome doesn't assimilate it.
Sorry cant include other parts of the code, but its confidential.
#*Html Part *#
<table class="table table-bordered">
<tr style="background-color:black">
<td align="center" style="color:white">Lunes</td>
<td align="center" style="color:white">Martes</td>
<td align="center" style="color:white">Miercoles</td>
<td align="center" style="color:white">Jueves</td>
<td align="center" style="color:white">Viernes</td>
<td align="center" style="color:white">Sabado</td>
<td align="center" style="color:white">Domingo</td>
</tr>
<tbody>
#for (int a = 1; a < dayOfWeekFirst; a++)
{
<td align="center"> </td>
}
#for (int i = 1; i <= daysInCurrentMonth; i++)
{
DateTime renderedDay = new DateTime(firstDay.Year, firstDay.Month, i);
<td align="center">#i</td>
if (renderedDay.DayOfWeek == DayOfWeek.Sunday)
{
//// Next Row
<tr />
}
}
#for (int a = 1; a <= 7 - dayOfWeekLast; a++)
{
<td align="center"> </td>
}
</tbody>
</table>
I don't believe the <tr /> is allowed as a row is expected to have cell elements.
Try adding an empty cell.
if (renderedDay.DayOfWeek == DayOfWeek.Sunday)
{
//// Next Row
<tr><td> </td></tr>
}
EDIT, then try closing the element with a closing tag. The tr element is not void-element in HTML so you shouldn't use <tr />.
if (renderedDay.DayOfWeek == DayOfWeek.Sunday)
{
//// Next Row
<tr></tr>
}
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
<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 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>