如何在 cshtml 页面的 p 标签中插入条件文本
How to insert conditional text in a p tag on a cshtml page
我的 Razor 应用程序有 cshtml 页面的这一部分:
<p style="text-align: justify">
thank you for confirming your reservation for the <b>@{ @Model.HotelChoiceDescription.Substring(@Model.HotelChoiceDescription.IndexOf("_") + 1) }</b>
</p>
if (!string.IsNullOrEmpty(Model.TypeOfRoomDescription))
{
<!-- Verify if english -->
if (Model.TypeOfRoomDescription.Contains("#STARTENG#") && Model.TypeOfRoomDescription.Contains("#ENDENG#"))
{
<p style="text-align: justify">
@(new HtmlString(Model.TypeOfRoomDescription.Substring(Model.TypeOfRoomDescription.IndexOf("#STARTENG#") + 10, Model.TypeOfRoomDescription.IndexOf("#ENDENG#") - Model.TypeOfRoomDescription.IndexOf("#STARTENG#") - 10))) for the period @Model.ReservationDate
</p>
}
<!-- default -->
if (!Model.TypeOfRoomDescription.Contains("#STARTENG#") && !Model.TypeOfRoomDescription.Contains("#STARTITA#"))
{
<p style="text-align: justify">
@(new HtmlString(Model.TypeOfRoomDescription)) for the period @Model.ReservationDate
</p>
}
}
结果是:
thank you for confirming your reservation for the Giant Hotel
Single room (eur. 44,00/night) for the period August 20, 2022 - August 27, 2022.
但是为了减少空间和打印页数,最好的结果是:
thank you for confirming your reservation for the Giant Hotel Single room (eur. 44,00/night) for the period August 20, 2022 - August 27, 2022.
同段所有文字
我试图将所有内容都放在第一个标签 p 中,但它也打印了 if 和“{”
我也试过输入@if,但它没有加载页面,因为它出错了
你可以尝试只用一个<p></p>
:
<p style="text-align: justify">
thank you for confirming your reservation for the <b>@{ @Model.HotelChoiceDescription.Substring(@Model.HotelChoiceDescription.IndexOf("_") + 1) }</b>
@if (!string.IsNullOrEmpty(Model.TypeOfRoomDescription))
{
<!-- Verify if english -->
if (Model.TypeOfRoomDescription.Contains("#STARTENG#") && Model.TypeOfRoomDescription.Contains("#ENDENG#"))
{
@(new HtmlString(Model.TypeOfRoomDescription.Substring(Model.TypeOfRoomDescription.IndexOf("#STARTENG#") + 10, Model.TypeOfRoomDescription.IndexOf("#ENDENG#") - Model.TypeOfRoomDescription.IndexOf("#STARTENG#") - 10)+" for the period "+Model.ReservationDate))
}
<!-- default -->
if (!Model.TypeOfRoomDescription.Contains("#STARTENG#") && !Model.TypeOfRoomDescription.Contains("#STARTITA#"))
{
@(new HtmlString(Model.TypeOfRoomDescription+" for the period "+Model.ReservationDate))
}
}
</p>
那么结果会在一行中:
thank you for confirming your reservation for the Giant Hotel Single room (eur. 44,00/night) for the period August 20, 2022 - August 27, 2022.
我的 Razor 应用程序有 cshtml 页面的这一部分:
<p style="text-align: justify">
thank you for confirming your reservation for the <b>@{ @Model.HotelChoiceDescription.Substring(@Model.HotelChoiceDescription.IndexOf("_") + 1) }</b>
</p>
if (!string.IsNullOrEmpty(Model.TypeOfRoomDescription))
{
<!-- Verify if english -->
if (Model.TypeOfRoomDescription.Contains("#STARTENG#") && Model.TypeOfRoomDescription.Contains("#ENDENG#"))
{
<p style="text-align: justify">
@(new HtmlString(Model.TypeOfRoomDescription.Substring(Model.TypeOfRoomDescription.IndexOf("#STARTENG#") + 10, Model.TypeOfRoomDescription.IndexOf("#ENDENG#") - Model.TypeOfRoomDescription.IndexOf("#STARTENG#") - 10))) for the period @Model.ReservationDate
</p>
}
<!-- default -->
if (!Model.TypeOfRoomDescription.Contains("#STARTENG#") && !Model.TypeOfRoomDescription.Contains("#STARTITA#"))
{
<p style="text-align: justify">
@(new HtmlString(Model.TypeOfRoomDescription)) for the period @Model.ReservationDate
</p>
}
}
结果是:
thank you for confirming your reservation for the Giant Hotel
Single room (eur. 44,00/night) for the period August 20, 2022 - August 27, 2022.
但是为了减少空间和打印页数,最好的结果是:
thank you for confirming your reservation for the Giant Hotel Single room (eur. 44,00/night) for the period August 20, 2022 - August 27, 2022.
同段所有文字
我试图将所有内容都放在第一个标签 p 中,但它也打印了 if 和“{” 我也试过输入@if,但它没有加载页面,因为它出错了
你可以尝试只用一个<p></p>
:
<p style="text-align: justify">
thank you for confirming your reservation for the <b>@{ @Model.HotelChoiceDescription.Substring(@Model.HotelChoiceDescription.IndexOf("_") + 1) }</b>
@if (!string.IsNullOrEmpty(Model.TypeOfRoomDescription))
{
<!-- Verify if english -->
if (Model.TypeOfRoomDescription.Contains("#STARTENG#") && Model.TypeOfRoomDescription.Contains("#ENDENG#"))
{
@(new HtmlString(Model.TypeOfRoomDescription.Substring(Model.TypeOfRoomDescription.IndexOf("#STARTENG#") + 10, Model.TypeOfRoomDescription.IndexOf("#ENDENG#") - Model.TypeOfRoomDescription.IndexOf("#STARTENG#") - 10)+" for the period "+Model.ReservationDate))
}
<!-- default -->
if (!Model.TypeOfRoomDescription.Contains("#STARTENG#") && !Model.TypeOfRoomDescription.Contains("#STARTITA#"))
{
@(new HtmlString(Model.TypeOfRoomDescription+" for the period "+Model.ReservationDate))
}
}
</p>
那么结果会在一行中:
thank you for confirming your reservation for the Giant Hotel Single room (eur. 44,00/night) for the period August 20, 2022 - August 27, 2022.