how to create an else if statement in Razor?

后端 未结 3 1225
终归单人心
终归单人心 2020-12-09 16:11

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 ma

相关标签:
3条回答
  • 2020-12-09 16:24

    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 })
        }
    else
        {
        @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;
    }
    
    0 讨论(0)
  • 2020-12-09 16:32

    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.

    0 讨论(0)
  • 2020-12-09 16:37

    as the question is for else if in razor, i used this for 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
        {
            ViewBag.Title = "home";
            ViewBag.Description = "home";
            ViewBag.Keywords = "home";
        }
    
    }
    

    hope helps someone.

    0 讨论(0)
提交回复
热议问题