ASP .NET核心剃刀视图条件显示不能按预期工作

时间:2017-01-11 17:35:05

标签: asp.net-mvc razor asp.net-core-mvc

我有一个视图,我有条件地迭代和打印项目:不显示SPAN(如预期的那样)。

<div>
@if (Model.SomeCondition)
{
    @foreach (var x in Model.SomeData)
    {
        <span>@x.Title</span>
    }
}
</div>

现在我不想显示封闭的DIV,但它不起作用:SPAN仍未显示,但DIV是。为什么会这样?

@if (Model.SomeCondition)
{
<div>
    @foreach (var x in Model.SomeData)
    {
        <span>@x.Title</span>
    }
</div>
}

这是完整的视图代码:

@using Team.L
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
@model Team.ViewModels.DeptTaskGroupView

@{
    var EventHandler = "Aventin." + ViewBag.AspAction + "(event)";
}

<form asp-controller="ModulesEx" asp-action="@ViewBag.AspAction" onsubmit="return false">
    <div class="form-horizontal">
        <input type="hidden" asp-for="Errors">
        <div class="MessageBox">@Html.Raw(Utility.GetMessage(Model))</div>
        <div class="form-group col-md-offset-2">
            <label class="col-md-2"></label>
            <div class="col-md-10">
                <span data-valmsg-for="" class="text-danger" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-md-2 control-label">@MyText.Department</label>
            <div class="col-md-10">
                <select asp-for="DepartmentID" class="form-control" asp-items="@Model.DepartmentList" onchange="@EventHandler"></select>
                <span asp-validation-for="DepartmentID" class="text-danger" />
            </div>
        </div>
        @if (Model.AssignedTaskGroup != null)
        {
        <div class="form-group">
            <label class="col-md-2 control-label">@MyText.TaskGroup</label>
            <div class="col-md-10">
                @foreach (var x in Model.AssignedTaskGroup)
                {
                    <input type="checkbox" name="selectedItems" value="@x.ID" @(Html.Raw(x.Assigned ? "checked=\"checked\"" : "")) />
                    @x.Title
                }
            </div>
        </div>
        }
        <div class="form-horizontal">
            <div class="form-group">
                <div class="col-md-1 col-md-offset-2">
                    <input type="submit" value="@MyText.Save" class="btn btn-default" />
                </div>
            </div>
        </div>
    </div>
</form>

1 个答案:

答案 0 :(得分:0)

如上所述,它不会发生。我猜你要么没有重新加载页面,要么你的演示代码周围有代码呈现这个div所包含的div。