2011-11-19 103 views
16

我想在表格中显示一些行。根据用户组,视图应显示不同的标记。管理员可以删除行,但主持人只能将其标记为可见或不可见。如何在Razor中创建else if语句?

如何在Razor中编写正确的if else语句?

页面显示正确,但在网页标题是解析错误

这是我的代码:

@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> 
+0

既然你说这个页面显示正确,那么关于视图内的逻辑或页面**标题**的问题呢? – JustinStolle

+0

逻辑必须是错误的,因为它给出了错误的标题? – Kenci

回答

25

标题有分析错误,因为你没有设置标题:

@{ 
    ViewBag.Title = "Home Page"; 
} 

现在的else语句,不要使用回@语法:

@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" }) 
} 

您正在检查布尔值,您只需要一个布尔值。对于else if也是如此。

你的代码可以通过只是做更简单:

@Html.TextBoxFor(bs => Model.Row_Description, new { @class = "rowdesc", size = 45, disabled = visible ? "" : "disabled" }) 

因为你反正都显示相同的代码,只是改变基于值的属性。对我来说,这变得更可读。

3

你只需要使用else没有前置一个@。但是,我不认为这是你的页面标题问题。也许你需要设置ViewBag.Title属性?您的布局页面可能取决于它的设置。

@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" }) 
    } 

标题问题:

@{ 
    bool visible = Model.Visible; 
    ViewBag.Title = "My Title; 
} 
+0

好吧,我纠正了它,但我仍然得到解析错误标题。怎么来的? – Kenci

+0

您更正了'ViewBag.Title'?你看到我原来的答案后,我可能补充说。 – tvanfosson

4

因为这个问题是别人如果剃须刀,我用这个搜索引擎优化的目的

鉴于

@{ 
    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"; 
    } 

} 

希望可以帮助别人。