Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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;
    @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" })
    @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" })
        @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)
                Since you say the page is displayed correctly, is the question about the logic within the view or the title of the page?
– JustinStolle
                Nov 19, 2011 at 22:07
@if(visible) 
    Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45 })
    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.

As the question is for else if in Razor page, For example, I used below for some SEO purpose.

In view

if (ViewBag.Option == "Mobiles") ViewBag.Title = "Mobiles"; ViewBag.Description = "Mobiles"; ViewBag.Keywords = "Mobiles"; else if (ViewBag.Option == "holiday") ViewBag.Title = "holiday"; ViewBag.Description = "holiday."; ViewBag.Keywords = "holiday"; else if (ViewBag.Option == "Laptops") ViewBag.Title = "Laptops"; ViewBag.Description = "Laptops."; ViewBag.Keywords = "Laptops"; ViewBag.Title = "home"; ViewBag.Description = "home"; ViewBag.Keywords = "home";

Hope helps someone.

You just use else without prepending an @. However, I don't think that is the problem with your page title. Perhaps you need to set the ViewBag.Title property? Your layout page may be depending on it being set.

@if(visible) 
    @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1 })
    @Html.TextBoxFor(cn => Model.Row_Number, new { @class = "row required digits", size = 1, disabled = "disabled" })

Title issue:

bool visible = Model.Visible; ViewBag.Title = "My Title; You corrected the ViewBag.Title? I may have added that after you saw my original answer. – tvanfosson Nov 19, 2011 at 22:11

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.