Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Statements on Cshtml page with Razor

I can't seem to get this code to work. How do I get the TextBoxFor to show on the screen? Nothing I try works.

@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>
like image 992
dellyjm Avatar asked Jul 21 '14 17:07

dellyjm


1 Answers

You should create a 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>
like image 126
Patrick Hofman Avatar answered Oct 05 '22 13:10

Patrick Hofman