如何创建if语句,还有一个在剃刀? [英] how to create an else if statement in Razor?

查看:101
本文介绍了如何创建if语句,还有一个在剃刀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个表中显示某些行。根据不同的用户组,查看应该表现出不同的标记。管理员可以删除行,但主持人只能将其标记为可见或不可见。

我怎样写正确的,如果在剃刀else语句?

正确显示网页,但网页标题为解析错误

这是我的code:

  @model MvcApplication3.Models.ViewModels.New.Question.MatrixRows@ {
    bool的可见= Model.Visible;
}&所述; TR>
    &所述; TD>
    @if(可见)
        {
        @ Html.TextBoxFor(CN => Model.Row_Number,新{@class =行所需要的数字,大小= 1})
        }
    @if(!可见)
        {
        @ Html.TextBoxFor(CN => Model.Row_Number,新{@class =行所需要的数字,大小= 1,禁用=禁用})
        }
    < / TD>
    &所述; TD>
    @if(可见)
        {
            @ Html.TextBoxFor(BS => Model.Row_Description,新{@class =rowdesc,大小= 45})
        }
    @if(!可见)
    {
        @ Html.TextBoxFor(BS => Model.Row_Description,新{@class =rowdesc,大小= 45,禁用=禁用})
    }
    < / TD>
    &所述; TD>
        @if(HttpContext.Current.User.IsInRole(管理员))
        {
            @ Html.HiddenFor(X => x.Delete,新{@class =标志换删除})
            @ Html.LinkToRemoveNestedForm(Slet,TR,input.mark换删除)
        }
        @if(HttpContext.Current.User.IsInRole(版主))
        {
            @ Html.HiddenFor(X => x.Visible,新{@class =标记为可见})
            @ Html.LinkToDisableNestedForm(Deaktiver,TR,input.mark换可见)
        }
        @ Html.HiddenFor(ID => Model.Row_Id)
    < / TD>
< / TR>


解决方案

标题有解析错误,因为你没有设置一个标题:

  @ {
    ViewBag.Title =主页;
}

现在一个else语句,不要使用背面的 @ 语法:

  @if(可见光)
{
    Html.TextBoxFor(BS => Model.Row_Description,新{@class =rowdesc,大小= 45})
}
其他
{
    Html.TextBoxFor(BS => Model.Row_Description,新{@class =rowdesc,大小= 45,禁用=禁用})
}

您正在检查一个布尔值,你只需要一个人。也为否则,如果,它的工作原理是一样的。

您code可由只是做更简单:

  @ Html.TextBoxFor(BS => Model.Row_Description,新{@class =rowdesc,大小= 45,禁用=可见的?:已禁用})

由于你反正显示相同code,只是改变基于值的属性。对我来说,这将成为更具可读性。

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 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语句,还有一个在剃刀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆