You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

若文本框含<br>标签则条件需为假的代码实现问询

Validation of Your Razor Code Against the Requirement

Let's break down your current code and how well it meets your stated requirement:

Current Code Limitation

Your implementation has two checks:

  1. Ensures notificationTextEnrolmentFeeSection is not null or empty
  2. Ensures the string is exactly equal to &lt;br&gt; (the HTML-encoded version of <br>)

This only handles the narrow case where the text box contains nothing but the <br> tag. However, your requirement specifies the condition should be false if the text box contains the <br> tag at all—even if it's part of a longer string. For example, your current code would still render the table for strings like "Payment details &lt;br&gt; See below", which violates the requirement.

Correct Implementation

To fix this, we need to check if the string does not contain the <br> tag (in its encoded form, as your original code uses):

@if (!string.IsNullOrEmpty(notificationTextEnrolmentFeeSection) && !notificationTextEnrolmentFeeSection.Contains("&lt;br&gt;"))
{
    <table class="reports-table table-striped table-bordered table-responsive page-break-after-avoid">
        <tr class="rowPrint">
            <th>
                <div class="font-bold letter-spacing">Fee Section</div>
            </th>
        </tr>
        <tbody>
            <tr class="rowPrint">
                <td>
                    <div>
                        @Html.Raw(notificationTextEnrolmentFeeSection) <!-- Optional: Use to render HTML content instead of encoded text -->
                    </div>
                </td>
            </tr>
        </tbody>
    </table>
}

Key Adjustments:

  • Replaced the exact equality check (!= "&lt;br&gt;") with a substring check (!Contains("&lt;br&gt;")) to catch any occurrence of the <br> tag
  • Added a note about @Html.Raw: use this if you want to render the actual HTML content instead of displaying encoded text like &lt;br&gt;
  • If your notificationTextEnrolmentFeeSection stores the raw <br> tag (not encoded), replace "&lt;br&gt;" with "
    " in the Contains check

Edge Cases Handled:

  • Skips rendering if the string is null or empty
  • Skips rendering if the string includes the <br> tag in any form
  • Renders the table only when the string has valid content without any <br> tags

内容的提问来源于stack exchange,提问作者Amit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:38:50