1

Below is the code i am using for one of my mvc views, within the mentioned if condition, html code is not recognized.

<table class="table">
        <tr class="row h4">
            <td>Task</td>
            <td>Parameter</td>
            <td>Active</td>
            <td><input type="button" class="btn btn-default" value="Add Mapping" /></td>
        </tr>
        @foreach (OrderTypeTaskParameterMapping mapping in Model.OrderTypeTaskParameterMappings)
        {
            <tr class="row">
                <td>
                    <select class="form-control">
                        @foreach (Task task in Model.Tasks)
                        {
                            <option value="@task.Id"
                                    @if (mapping.TaskId == task.Id)
                                    {
                                        selected="selected" **doesn't work**
                                    }
                                    >@task.Name</option>
                        }
                    </select>
                </td>
                <td>
                    <select class="form-control">
                        @foreach (Parameter p in Model.Parameters)
                        {
                            <option>@p.Name</option>
                        }
                    </select>
                </td>
                <td>
                    @if (mapping.Active)
                    {
                        <input type="button" class="btn btn-info" value="Active" />
                    }
                    else
                    {
                        <input type="button" class="btn btn-danger" value="InActive" />
                    }
                </td>
                <td><input type="button" class="btn btn-info" value="Save" /></td>
            </tr>
        }
    </table>

On executing the above code i get an error : enter image description here

What could be the reason? I thought this should have worked. Am i missing something here?

4
  • Use the strongly typed @Html.DropDownListFor() helper so your html is constructed correctly. Commented Apr 16, 2015 at 12:24
  • @StephenMuecke Thanks for the work around. Could you still point out the issue in the original code. Commented Apr 16, 2015 at 12:27
  • Downvoter, can you please let me know the reason? Isn't this as per stackoverflow.com/a/14569710/793784 Commented Apr 16, 2015 at 12:28
  • 1
    It will work if you use @:selected="selected" or <text>selected="selected"</text> And using the html helpers is not a 'workaround'! its the correct way. you code is truly awful Commented Apr 16, 2015 at 12:40

2 Answers 2

1

I think it also works using conditional attributes:

<option value="@task.Id" selected="@(mapping.TaskId == task.Id)">

If the expression evaluates to false, then Razor will not output the selected attribute.

More on conditional attributes

About the original question, I believe the problem is related to how Razor tells HTML from C# blocks. On your code, you are not closing the option tag. You're trying to open it, write (or not) the selected attribute using a code block and then close it.

Inside the code block, Razor's expecting a line of code or a new HTML tag. As it's not an HTML tag (no angle bracket) it tries to interpret it as C#, hence the missing ; message.

That's why the <tr> and <td> tags inside the parent @foreach(){}} are working: because they can be interpreted as new HTML blocks.

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

2 Comments

Thanks for the answer, i got multiple answers to this question and now i am trying to find out what is wrong with the original code.
Updated the answer with more information about what I think is wrong on the original code.
0

Try this code

<option value="@task.Id" @(mapping.TaskId == task.Id ? "selected" : string.Empty)>

Inside {} you can use only C# code

3 Comments

Is it? What about the whole bunch of <tr> <td> tags i am using inside the parent @foreach(){} . That surely works!
I tried your code, that looks like something i should have done. It works! Still i would like to know what wrong in the original code. Thanks for the answer!
Sorry Jacek, Answer by Anderson looks more relevant to me, so shifting the marked answer.

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.