Reputation: 47743
I'm trying to show one form vs. the other depending on the if statement. But when this runs, both forms show and also the if statement itself is showing as literal text instead:
if(model.CarId == 0 || string.IsNullOrEmpty(model.Car))
{
@using (Html.BeginForm("CarModels", "Users", FormMethod.Post, new { @model = Model }))
{
<p>Car Model @Html.TextBoxFor(model => model.CarModel)</p>
<p><input type="submit" value="Find Model" /></p>
}
}
else
{
@using (Html.BeginForm("UpdateCar", "Cars", FormMethod.Post, new { @model = Model }))
{
<p>@Html.CheckBoxFor(model => model.Name) Name</p>
<p><input type="submit" value="Update" /></p>
}
}
I know I need an @ next to if but tried that and then the view coplains about the rest of the syntax inside the @if when I do that.
UPDATE
Here's what finally worked:
@{
if (Model.CarId == 0 || string.IsNullOrEmpty(Model.Car))
{
using (Html.BeginForm("CarModels", "Users", FormMethod.Post, new { model = Model }))
{
<p>Car Model @Html.TextBoxFor(model => model.CarModel)</p>
<p><input type="submit" value="Find Model" /></p>
}
}
else
{
using (Html.BeginForm("UpdateCar", "Cars", FormMethod.Post, new { model = Model }))
{
<p>@Html.CheckBoxFor(model => model.Name) Name</p>
<p><input type="submit" value="Update" /></p>
}
}
}
Upvotes: 0
Views: 167
Reputation: 6862
Consider changing your code like this:
@{
if (Model.CarId == 0 || string.IsNullOrEmpty(Model.Car))
{
using (Html.BeginForm("CarModels", "Users", FormMethod.Post, new { @model = Model }))
{
<p>Car Model @Html.TextBoxFor(model => model.CarModel)</p>
<p><input type="submit" value="Find Model" /></p>
}
}
else
{
using (Html.BeginForm("UpdateCar", "Cars", FormMethod.Post, new { @model = Model }))
{
<p>@Html.CheckBoxFor(model => model.Name) Name</p>
<p><input type="submit" value="Update" /></p>
}
}
}
The "@{ .. }" block is used to insert arbitrary number of code lines.
Upvotes: 0
Reputation: 1701
You have to add a @ before the if and remove it before the using, as it is still considered as code instead of markup.
Upvotes: 2