I am trying to display some rows in a table. Depending on the UserGroup, the View should show different markup. An administrator can delete rows, but a moderator can only mark them as visible or invisible.
How do i write a proper if else statement in Razor?
The page is displayed correctly, but the page title is Parse Error
This is my code:
@model MvcApplication3.Models.ViewModels.New.Question.MatrixRows @{ bool visible = Model.Visible; } <tr> <td> @if(visible) { @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1 }) } @if (!visible) { @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1, disabled = "disabled" }) } </td> <td> @if(visible) { @Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45 }) } @if (!visible) { @Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45, disabled = "disabled" }) } </td> <td> @if (HttpContext.Current.User.IsInRole("Administrator")) { @Html.HiddenFor(x => x.Delete, new { @class = "mark-for-delete" }) @Html.LinkToRemoveNestedForm("Slet", "tr", "input.mark-for-delete") } @if (HttpContext.Current.User.IsInRole("Moderator")) { @Html.HiddenFor(x => x.Visible, new { @class = "mark-for-visible" }) @Html.LinkToDisableNestedForm("Deaktiver", "tr", "input.mark-for-visible") } @Html.HiddenFor(id => Model.Row_Id) </td> </tr>
The if statement returns true or false, based on your test: The if statement starts a code block. The condition is written inside parenthesis. The code inside the braces is executed if the test is true.
The @ symbol is used in Razor initiate code, and tell the compiler where to start interpreting code, instead of just return the contents of the file as text. Using a single character for this separation, results in cleaner, compact code which is easier to read.
Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine.
The title has parse error because you did not set a title:
@{ ViewBag.Title = "Home Page"; }
now for an else statement, don't use back the @
syntax:
@if(visible) { Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45 }) } else { Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45, disabled = "disabled" }) }
You are checking for a boolean, you just need an else. Also for else if
, it works the same.
Your code could be simplified even more by just doing:
@Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45, disabled = visible ? "" : "disabled" })
Because you are displaying the same code anyways, just changing the attribute based on a value. To me, this becomes more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With