Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
hi Guys!
I am facing problem i want to add Tr if the condition is true and if the condition is false it will add td in existing tr but it will give an error td not nested in table.
foreach (var r in Model)
        {

            var stationName = "";

            if (r.stationName != stationName)
            {
                <tr class="row-2 even">

                    <td class="Cell2">@r.stationName</td>

                    <td class="Cell2 kara">@if (r.cityId == 1)
                    {@r.contactNo}</td>

                    <td class="Cell2 lhr">@if (r.cityId == 2)
                    {@r.contactNo}</td>

                    <td class="Cell2 isl">@if (r.cityId == 3)
                    {@r.contactNo}</td>

                    <td class="Cell2 psh">@if (r.cityId == 4)
                    {@r.contactNo}</td>

                    <td class="Cell2 qut">@if (r.cityId == 5)
                    {@r.contactNo}</td>

                    <td class="Cell2 fas">@if (r.cityId == 6)
                    {@r.contactNo}</td>

                      </tr>
                    }
                    else
                    {
                    <td></td>

                    }
              
                stationName = r.stationName;
            }
        }


What I have tried:

How can i solve this problem any guide please.
Posted
Updated 11-Nov-17 9:40am
Comments
Karthik_Mahalingam 11-Nov-17 9:57am    
what is the error message ?

You are missing the @ symbol before foreach keyword, which should give you syntax error.

HTML
@foreach (var r in Model)


Hope, it helps :)
 
Share this answer
 
Your tr must be placed outside if condition.

foreach (var r in Model)
        {
 
            var stationName = "";
 <tr class="row-2 even">
 
                  
            if (r.stationName != stationName)
            {
                <td class="Cell2">@r.stationName</td>
}
else
{
<td></td>
}
</tr>
 
 
Share this answer
 
v2
Comments
Member 13461215 13-Nov-17 2:32am    
Yes but it will create new row each time when loop executed. i want to insert new row only when the condition is false.
Chirag Sudra 13-Nov-17 9:37am    
You need to add tr in both if else condition as td cannot be created with tr.
No i have symbol in my code my question is that at the last in else statement TD tag is not working it gives me an error td is not nested in table.
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 11-Nov-17 9:56am    
is this solution or comment?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900