带有Razor的Cshtml页面上的条件语句

6

我无法让这段代码运行。我该如何让屏幕显示TextBoxFor?我尝试过的方法都无效。

@foreach (var items in Model.Pages[0].Items){
<div class="form-group">
<label for="pageType" class="col-sm-2 control-label">Label:</label>

<div class="col-sm-10">
    @{
       string htmlOutput;
       if (items.PageItemTypeId == (int)HOD.Controllers.PageItemTypesEnum.MainTextContent)
       {
           htmlOutput = @Html.TextBoxFor(x => items.PageContent, new { @class = "form-control", @placeholder = "Content" }).ToHtmlString();
           Response.Write(htmlOutput);
       }
  <input type="hidden" id="pageTypeId" />
</div>
</div>
1个回答

17

你应该创建一个Razor @if语句

<div class="col-sm-10">
    @if (items.PageItemTypeId == (int)HOD.Controllers.PageItemTypesEnum.MainTextContent)
    {
        @Html.TextBoxFor(x => items.PageContent, new { @class = "form-control", @placeholder = "Content" });
    }
  <input type="hidden" id="pageTypeId" />
</div>

谢谢,我有一个瞬间回到了幼儿园,没有检查数据。语法是正确的。 - dellyjm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接