Home > Back-end >  How to insert conditional text in a p tag on a cshtml page
How to insert conditional text in a p tag on a cshtml page

Time:03-16

I Have this portion of cshtml page part of my Razor application:

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

The result is:

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.

But to reduce the spaces and consequently the pages to be printed, the best result would be this:

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.

All the text in the same paragraph

I tried to put everything inside the first tag p but it also prints the if and the "{" I also tried entering @if, but it doesn't load the page because it goes in error

CodePudding user response:

You can try to only use a <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>

then the result will be in a line:

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.
  • Related