How to use if statement inside of foreach loop in MVC

sakthivel

New member
Joined
Jun 29, 2021
Messages
2
Programming Experience
1-3
I don't know how to work. SO please let me know if anybody have answer.Thanks.

Html Page:
C#:
@{
    ViewBag.Title = "DetailsIND";
    var seqNO = 1;
    var seqNNo = 1;
    var flag = "";
}

@{
    var seqNO = 0;
    var seqNNo = 1;
    var flag = "";
}

<table>

        @foreach (var item in Model.order_details.GroupBy(x => x.delivery_add).Select(x1 => x1.FirstOrDefault()).ToList())
        {
            var seqno_ = seqNO++;
            var a2 = seqNNo++;
            if (seqno_ % 2 == 0)
            {
                flag = "Even";
            }
            else
            {
                flag = "Odd";
            }

            if (flag == "Odd")
            {
                <text> <tr></text>
                <td>
                <b>Delivery Address @a2: </b>
            </td>
            }
            else
            {
                           <td>
                               <b>Delivery Address @a2: </b>
                           </td>
                 <text></tr></text>
            }
          }
</table>

Output:
C#:
Delivery Address 1:             Delivery Address 2:
 
Last edited by a moderator:
Welcome to the C# forums. In the future, please put your code in code tags. Also for any output where you want to preserve the formatting, use code tags around them as well.
 
What problem are you running into?
 
i have mentioned my current code and wanted output.

C#:
@{
    ViewBag.Title = "DetailsIND";
    var seqNO = 1;
    var seqNNo = 1;
    var flag = "";
}

@{  
    var seqNO = 0;
    var seqNNo = 1;
    var flag = "";
}

<table>

        @foreach (var item in Model.order_details.GroupBy(x => x.delivery_add).Select(x1 => x1.FirstOrDefault()).ToList())
        {
            var seqno_ = seqNO++;
            var a2 = seqNNo++;
            if (seqno_ % 2 == 0)
            {
                flag = "Even";
            }
            else
            {
                flag = "Odd";
            }

            if (flag == "Odd")
            {
                <text> <tr></text>
                <td>
                <b>Delivery Address @a2: </b>
            </td>
            }
            else
            {
                           <td>
                               <b>Delivery Address @a2: </b>
                           </td>
                 <text></tr></text>
            }
          }
</table>

Output:
ODD Even
Delivery Address:1 Delivery Address:2
Delivery Address:3 Delivery Address:4
 
Yes, you did that. But we are NOT a code writing service. We will not do your work for you. If you tell us what problem you are running into, we can guide you towards a solution.
 
Back
Top Bottom