9

else ブロックでのプレーン テキストの表示 (表示ではなく) に問題があります。

if (Model.CareerFields != null && ViewBag.CFCount > 0)
{
<h3>Careerfields Listing</h3>

<table>
   <tr>
      <th></th>
      <th>Careerfield Name</th>
   </tr>

   @foreach (var item in Model.CareerFields)
   {
       <tr>
       <td>
          @Html.ActionLink("Select", "Index", new { careerFieldID = item.CareerFieldId })
       </td>
       <td>
          @item.CareerFieldName
       </td>
       </tr>
   }
   </table>
}
else
{
  No Careerfields associated with @ViewBag.SelectedDivisionTitle
}

if ブロックは正常に機能します。テキストは true の場合にのみレンダリングされます。ただし、else ブロックのテキストは、false と評価された場合だけではなく、ページの読み込み時にレンダリングされます。

使ってみました

Hmtl.Raw("No Careerfields associated with ")
<text>No Careerfields associated with @ViewBag.SelectedDivisionTitle</text>
@:No Careerfields associated with @ViewBag.SelectedDivisionTitle

ただし、評価前に平文をレンダリングします。

助言がありますか?

4

3 に答える 3

11

「プレーンテキスト」を裸の<span>タグの中に入れます:

else
{
  <span>No Careerfields associated with @ViewBag.SelectedDivisionTitle</span>
}

ブラウザーはそれを特別にレンダリングするべきではありません (すべてのスパンを選択する css がない限り)。これにより、かみそりが C# の終わりを感知して HTML を出力するのに役立ちます。

于 2012-08-01T21:21:19.147 に答える
2

声明の@前にサインを忘れたようです。ifこれを試して:

@if (Model.CareerFields != null && ViewBag.CFCount > 0)
{
    <h3>Careerfields Listing</h3>

    <table>
        <tr>
            <th></th>
            <th>Careerfield Name</th>
        </tr>

        @foreach (var item in Model.CareerFields)
        {
            <tr>
                <td>
                    @Html.ActionLink("Select", "Index", new { careerFieldID = item.CareerFieldId })
                </td>
                <td>@item.CareerFieldName</td>
            </tr>
        }
    </table>
}
else
{
    <text>No Careerfields associated with @ViewBag.SelectedDivisionTitle</text>
}
于 2012-11-02T10:31:32.103 に答える