条件ASP.NET MVC剃刀部分

时间:2022-08-26 20:48:55

I want to define this section only if some property (Model.ReadOnly) is false.

我想仅在某些属性(Model.ReadOnly)为false时才定义此部分。

@section toolbar {
    <div class="tool">
        <div class="row">
            @Html.ActionLink( Resources.Strings.Edit, "Edit", "Profile" )
        </div>
        <div class="row">
            @Html.ActionLink( Resources.Strings.Delete, "Delete", "Profile" )
        </div>
    </div >
}

I tried wrapping it up in @if ( !Model.ReadOnly ) {} but it doesn't work.

我尝试将其包装在@if(!Model.ReadOnly){}中,但它不起作用。

Is there a way to do this?

有没有办法做到这一点?


I do not want to define an empty section (as @itsmatt suggests), the layout of my page changes whether the section is defined or not (using IsSectionDefined( "toolbar" )).

我不想定义一个空的部分(如@itsmatt建议的那样),我的页面布局会改变是否定义了部分(使用IsSectionDefined(“toolbar”))。

3 个解决方案

#1


38  

This should work.

这应该工作。

@if (!Model.ReadOnly)
{
    <text>
    @section toolbar {

    }
    </text>
}

I never said it would be pretty ;-)

我从来没说过会很漂亮;-)

#2


3  

This works for me:

这对我有用:

@section SomeSection {
   @if (!Model.ReadOnly)
   {

   }
}

Essentially flipping where the conditional is. This essentially results in an empty section if Model.ReadOnly is true.

基本上是在有条件的地方翻转。如果Model.ReadOnly为true,这实际上会导致空部分。


Update:

更新:

So, what about moving that section to a PartialView and doing something like:

那么,将该部分移动到PartialView并执行以下操作:

@Html.Partial("MyAction")

in your View and then let the MyAction return you the appropriate PartialView based on the ReadOnly value? Something like:

在您的视图中,然后让MyAction根据ReadOnly值返回相应的PartialView?就像是:

public PartialViewResult MyAction()
{
   ...

   // determine readonly status - could have passed this to the action I suppose    
   if (ReadOnly)
   {
      return PartialView("TheOneThatDefinesTheSection");
   }
   else
   {
      return PartialView("TheOneThatDoesNotDefineTheSection");
   }
}

Seems like that would work just fine.

似乎这样可以正常工作。

#3


0  

Bertrand,

伯特兰

See:

看到:

Razor If/Else conditional operator syntax

Razor If / Else条件运算符语法

basically (para-phrasing), ... Razor currently supports a subset of C# expressions without using @() and unfortunately, ternary operators are not part of that set.

基本上(para-phrasing),... Razor目前支持不使用@()的C#表达式子集,不幸的是,三元运算符不属于该集合。

also, this may be a way around the issue:

此外,这可能是解决问题的方法:

conditional logic in mvc view vs htmlhelper vs action

mvc视图中的条件逻辑vs htmlhelper vs action

basically, use the if logic to call a partialview to satisfy your criteria.

基本上,使用if逻辑调用partialview来满足您的标准。

[edit] this was my basic thinking (where your @section code is defined in that partial):

[编辑]这是我的基本思路(你的@section代码在那个部分定义):

@if(!Model.ReadOnly) 
{
    @Html.Partial("toolbar")
}

#1


38  

This should work.

这应该工作。

@if (!Model.ReadOnly)
{
    <text>
    @section toolbar {

    }
    </text>
}

I never said it would be pretty ;-)

我从来没说过会很漂亮;-)

#2


3  

This works for me:

这对我有用:

@section SomeSection {
   @if (!Model.ReadOnly)
   {

   }
}

Essentially flipping where the conditional is. This essentially results in an empty section if Model.ReadOnly is true.

基本上是在有条件的地方翻转。如果Model.ReadOnly为true,这实际上会导致空部分。


Update:

更新:

So, what about moving that section to a PartialView and doing something like:

那么,将该部分移动到PartialView并执行以下操作:

@Html.Partial("MyAction")

in your View and then let the MyAction return you the appropriate PartialView based on the ReadOnly value? Something like:

在您的视图中,然后让MyAction根据ReadOnly值返回相应的PartialView?就像是:

public PartialViewResult MyAction()
{
   ...

   // determine readonly status - could have passed this to the action I suppose    
   if (ReadOnly)
   {
      return PartialView("TheOneThatDefinesTheSection");
   }
   else
   {
      return PartialView("TheOneThatDoesNotDefineTheSection");
   }
}

Seems like that would work just fine.

似乎这样可以正常工作。

#3


0  

Bertrand,

伯特兰

See:

看到:

Razor If/Else conditional operator syntax

Razor If / Else条件运算符语法

basically (para-phrasing), ... Razor currently supports a subset of C# expressions without using @() and unfortunately, ternary operators are not part of that set.

基本上(para-phrasing),... Razor目前支持不使用@()的C#表达式子集,不幸的是,三元运算符不属于该集合。

also, this may be a way around the issue:

此外,这可能是解决问题的方法:

conditional logic in mvc view vs htmlhelper vs action

mvc视图中的条件逻辑vs htmlhelper vs action

basically, use the if logic to call a partialview to satisfy your criteria.

基本上,使用if逻辑调用partialview来满足您的标准。

[edit] this was my basic thinking (where your @section code is defined in that partial):

[编辑]这是我的基本思路(你的@section代码在那个部分定义):

@if(!Model.ReadOnly) 
{
    @Html.Partial("toolbar")
}