locked
making a tag behave as a tag (it is being treated as string characters) RRS feed

  • Question

  • User120130202 posted

    sb.Append("Hi ho<br />hi ho!");

    it is displaying as

    Hi ho<br />hi ho!

    I would like the <br /> to work as a tag.  I am working with a razor page.

    -----

    the answer (given to me below) was to use @Html.Raw on the cshtml page.

    @Html.Raw(Model.dwarf)
    Saturday, January 18, 2020 3:06 PM

Answers

  • User475983607 posted

    KerryK

    I'm on a cshtml.cs page

    I tried the above and got "html" does not exist in the current context.  Below is the entire method, in case that helps.

    public void DoStuff()
    {
    StringBuilder sb = new StringBuilder();

    sb.Append("Hi ho<br />hi ho!");

    dwarf = sb.ToString();
    }

    I assume this is ASP.NET Core Razor pages?  Keep in mind the community can only see the source code that you share.  It is important that you share all the code so we can see where you are going off the path and we can figure out what framework you are using.

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <div> @Html.Raw(Model.dwarf) </div>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.Extensions.Logging;
    
    namespace RazorDemo.Pages
    {
        public class IndexModel : PageModel
        {
            private readonly ILogger<IndexModel> _logger;
    
            public IndexModel(ILogger<IndexModel> logger)
            {
                _logger = logger;
            }
    
            public void OnGet()
            {
                DoStuff();
            }
    
            public void DoStuff()
            {
                StringBuilder sb = new StringBuilder();
    
                sb.Append("Hi ho<br />hi ho!");
    
                dwarf = sb.ToString();
            }
    
            public string dwarf { get; set; }
        }
    }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 18, 2020 3:39 PM

All replies

  • User475983607 posted

    KerryK

    sb.Append("Hi ho<br />hi ho!");

    it is displaying as

    Hi ho<br />hi ho!

    I would like the <br /> to work as a tag.  I am working with a razor page.

    Yeah, the framework encodes HTML characters to protect your application from malicious scripting attacks.  If you trust the variable, ASP.NET provides methods to render the HTML string as is. 

    @Html.Raw(sb.ToString())


     

    Saturday, January 18, 2020 3:12 PM
  • User120130202 posted

    I'm on a cshtml.cs page

    I tried the above and got "html" does not exist in the current context.  Below is the entire method, in case that helps.

    public void DoStuff()
    {
    StringBuilder sb = new StringBuilder();

    sb.Append("Hi ho<br />hi ho!");

    dwarf = sb.ToString();
    }

    Saturday, January 18, 2020 3:25 PM
  • User475983607 posted

    KerryK

    I'm on a cshtml.cs page

    I tried the above and got "html" does not exist in the current context.  Below is the entire method, in case that helps.

    public void DoStuff()
    {
    StringBuilder sb = new StringBuilder();

    sb.Append("Hi ho<br />hi ho!");

    dwarf = sb.ToString();
    }

    I assume this is ASP.NET Core Razor pages?  Keep in mind the community can only see the source code that you share.  It is important that you share all the code so we can see where you are going off the path and we can figure out what framework you are using.

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <div> @Html.Raw(Model.dwarf) </div>
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Microsoft.Extensions.Logging;
    
    namespace RazorDemo.Pages
    {
        public class IndexModel : PageModel
        {
            private readonly ILogger<IndexModel> _logger;
    
            public IndexModel(ILogger<IndexModel> logger)
            {
                _logger = logger;
            }
    
            public void OnGet()
            {
                DoStuff();
            }
    
            public void DoStuff()
            {
                StringBuilder sb = new StringBuilder();
    
                sb.Append("Hi ho<br />hi ho!");
    
                dwarf = sb.ToString();
            }
    
            public string dwarf { get; set; }
        }
    }
    
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 18, 2020 3:39 PM