.NET MVC Razor View:有条件地包含 razor 命令



我有这个编辑器模板:

@model MyNamespace.Models.SomeModel
@using HtmlHelpers.BeginCollectionItem 
<div>
@using (Html.BeginCollectionItem("someProperty")) {
    <div class="form-group">
        @Html.LabelFor(m => m.field1, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.PasswordFor(m => m.field1, new { @class = "form-control" })
        </div>
    </div>
    //more content here
}
</div>

我需要根据 ViewBag 的值有条件地包含@using (Html.BeginCollectionItem("someProperty")) {}部分。

这可能吗?

是的,这是可能的您需要在@ {/// using - without @ }中包含using

例:

@{
    if(ViewBag.x==x){
       using (Html.BeginCollectionItem("someProperty")) {}
    }else{
    }
}

最新更新