Search code examples
c#asp.netasp.net-mvcrazorrazor-pages

Can i use html code within "@()" in razor?


I want to use html code in ternary operator within @() brackets.

Example:

<h1 class="display-4">Welcome @(SignInManager.IsSignedIn(User) ? @user.FirstName : "Maybe you wanna log in?")</h1>

I want to make "Maybe you wanna log in?" into another line, with br/, /n or anything else. How can i do it without going to another line of code?


Solution

  • Use Html.Raw to prevent rendered HTML from getting encoded:

    <h1 class="display-4">Welcome @(SignInManager.IsSignedIn(User) ? user.FirstName : Html.Raw("Maybe you wanna<br> log in?"))</h1>