对于我所期望的人来说,这应该是一些简单的点,但作为一个前端开发人员,试图在对C#一无所知的情况下处理MVC剃刀,这让我感到困惑。
我有一个布尔变量hasSecond,我想在下面的foreach中考虑它
@foreach (PODO_AcceptRejectReasons reason in reasonList2.Where(e => e.Type == 1))
{
<option data-confirm-type="1" data-confirm-attr="@reason.Attribute" value="@reason.ID">@reason.Text</option>
}我只想在hasSecond为true时显示@reason.Atrribute值为'SECOND‘的选项,否则不显示这些选项。
谢谢你的帮忙!
发布于 2013-03-01 17:21:28
@foreach (PODO_AcceptRejectReasons reason in reasonList2.Where(e => e.Type == 1))
{
if(hasSecond||reason.Attribute!="SECOND")
{
<option data-confirm-type="1" data-confirm-attr="@reason.Attribute" value="@reason.ID">@reason.Text</option>
}
}应该能行得通。我想我之前的逻辑有点错误。这将显示reason.Attribute不是SECOND的所有option。如果为SECOND,则仅当hasSecond为true时才显示option。
发布于 2013-03-01 17:22:00
只需将其添加到Where语句:
@foreach (PODO_AcceptRejectReasons reason in reasonList2.Where(e => e.Type == 1 && hasSecond))
{
<option data-confirm-type="1" data-confirm-attr="@reason.Attribute" value="@reason.ID">@reason.Text</option>
}发布于 2013-03-01 17:19:41
您可以通过以下方式完成此操作,只需将if语句放入foreach循环中,并将另一个子句添加到where。
@foreach (PODO_AcceptRejectReasons reason in reasonList2.Where(e => e.Type == 1 && e.Attribute=="SECOND"))
{
if(hasSecond)
{
<option data-confirm-type="1" data-confirm-attr="@reason.Attribute" value="@reason.ID">@reason.Text</option>
}
}如果您只想在hasSecond为false的情况下删除data-confirm-attr="@reason.Attribute",则可以使用以下方法:
@foreach (PODO_AcceptRejectReasons reason in reasonList2.Where(e => e.Type == 1))
{
<option data-confirm-type="1" @if(hasSecond) { <text>data-confirm-attr="@reason.Attribute"</text> } value="@reason.ID">@reason.Text</option>
}https://stackoverflow.com/questions/15154239
复制相似问题