3

I must admit I am new to C# MVC 4 programming, but I've never seen such a weird thing in any other programming language...

The if part inside the foreach works good, however, even if it's true, the else part gets executed, which is very strange.

When I remove " @dag.Datum.ToString("dd/MM/yyyy") ", the if else works, but unfortunately that is not what I want.

Thanks in advance for any help!

(ow and sorry for the dutch I used in my code)

@using (Html.BeginForm())
{
    <br /><br /><table>
    @foreach (var cursist in Model.Cursisten.Where(cursist => cursist.Soort == 1))
    {
        <tr>

            <th>@cursist.Email</th>

            @foreach (var dag in Model.Opleidingsdagen.Where(m => m.chked == true))
            {
                <td>

                    @foreach (var afw in Model.Afwezigheden)
                    {
                        if (afw.DagID.Equals(dag.DagID) && afw.CursistID.Equals(cursist.Email))
                        {
                            @:a @dag.Datum.ToString("dd/MM/yyyy")
                        }
                        else
                        {
                            @:b
                        } 
                    }

                </td>
            }


        </tr>
    }
    <tr>
        <td><br /><br /><input type="submit" value="Afwezigheden aanpassen" class="btn" /></td>
    </tr>
    </table>
}

EDIT and this is the html table output I always get:

[email protected]     a 14/08/2013 b  b a 15/08/2013  b b
[email protected]     b b     b b     b b
[email protected]     b b     b b     b b

EDIT #2 my expected output is:

[email protected]     a 14/08/2013    a 15/08/2013 b
[email protected]     b               b            b
[email protected]     b               b            b

so there always is a 'b', even when an 'a + date ' is outputted

4
  • 2
    Is @:a and @:b really part of your syntax? Commented Aug 6, 2013 at 11:43
  • Can you reproduce it in a single razor file without any external dependencies, so that others can easily test it as well? Commented Aug 6, 2013 at 11:56
  • 1
    perhaps I'm confused but: I see 6 elements in each row; in row 1, two of them are 'a (date)' elements instead of 'b' elements. What did you expect it to output? Commented Aug 6, 2013 at 11:56
  • The output looks fine to me. It's doing exactly what the algorithm says? Commented Aug 6, 2013 at 12:37

2 Answers 2

4

There is no bug and it is working fine. You have 3 nested foreach loops, which produce 6 outputs per email address and 2 within each <td>, put a breakpoint in the code and follow the logic. I am 100% sure this is a bug on your end not MS.

Sign up to request clarification or add additional context in comments.

Comments

0

What happens if you try:

<text>a @dag.Datum.ToString("dd/MM/yyyy")</text>

in the if statement?

It's possible that the Razor parser is getting a bit confused and executing the else clause because of the format of the @:a @dag.Datum.ToString("dd/MM/yyyy") line.

EDIT:

What happens if you try

@foreach (var afw in Model.Afwezigheden)
{
    if (true)
    {
        @:a @dag.Datum.ToString("dd/MM/yyyy")
    }
    else
    {
        @:b
    } 
}

Is the else clause also executed?

EDIT:

Ahh OK, based on your comment, then it does seem that your if statement clause is returning false, otherwise I cannot see how the else clause can ever be executed. Extract each part into a variable:

bool idMatch = afw.DagID.Equals(dag.DagID);
bool emailMatch = afw.CursistID.Equals(cursist.Email);

if (idMatch && emailMatch)

It will be easier to determine which equality if failing.

1 Comment

@Bram, are you saying the if and the else get executed on every single iteration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.