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>
}
}
}
Consider changing your code like this:
The “@{ .. }” block is used to insert arbitrary number of code lines.