使用Razor在Cshtml页面上的条件语句

时间:2014-07-21 17:00:28

标签: c# asp.net-mvc asp.net-mvc-4 razor

我似乎无法使用此代码。如何让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 个答案:

答案 0 :(得分:13)

您应该创建Razor @if statement

<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>