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?

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

2 Answers2

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.

Anderson Pimentel
  • 5,086
  • 2
  • 32
  • 54
0

Try this code

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

Inside {} you can use only C# code

Jacek
  • 11,661
  • 23
  • 69
  • 123
  • Is it? What about the whole bunch of tags i am using inside the parent @foreach(){} . That surely works! – blogbydev Apr 16 '15 at 12:22
  • 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! – blogbydev Apr 16 '15 at 12:25
  • Sorry Jacek, Answer by Anderson looks more relevant to me, so shifting the marked answer. – blogbydev Apr 16 '15 at 14:49