locked
Format RTF from database in view RRS feed

  • Question

  • User-973886032 posted

    hi guys

    how do I format or html encode Rich Text Format in my database when presenting it to a user. e.g.

    Here is a sample code (As I am trying to get the <i>Jumps</i> values in italics

    // in my view I am displaying database values which have been formatted in RTF formats
     @Html.DisplayFor(model => model.B)
    
    
    
    /// my controller
       public async Task<IActionResult> Index(string Cat)
            {
    
    
                Cat = Cat.Replace("_", " ");
    
                ViewData["Cat"] = Cat;
                try
                    {
    
    
                    var x = await _context.B_Database
                   .FirstOrDefaultAsync(m => m.Name == Cat);
                    if (x == null)
                    {
                        return NotFound();
                    }
    
                    return View(x);
    
    
    
    /// my view results
    
    The quick brown fox <i>Jumps</i> over the lazy dog
    
    //

    Monday, August 5, 2019 10:23 PM

Answers

  • User475983607 posted

    Use @Html.Raw() if you trust the data.  Otherwise, post enough code that will allow the community to reproduce this issue.

    @Html.Raw("The quick brown fox <i>Jumps</i> over the lazy dog.")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2019 10:32 PM

All replies

  • User475983607 posted

    Use @Html.Raw() if you trust the data.  Otherwise, post enough code that will allow the community to reproduce this issue.

    @Html.Raw("The quick brown fox <i>Jumps</i> over the lazy dog.")

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 5, 2019 10:32 PM
  • User-474980206 posted

    google "rtf to html c#" to get the library of choice to convert the rtf text to html, then as suggested use Html.Raw() to display the html.

    Tuesday, August 6, 2019 2:18 PM
  • User-973886032 posted

    Use @Html.Raw() if you trust the data.  Otherwise, post enough code that will allow the community to reproduce this issue.

    @Html.Raw("The quick brown fox <i>Jumps</i> over the lazy dog.")

    I simply used this and it worked


    @foreach (var item in Model) {

    @Html.Raw(item.B) // This worked

    As the default label obstructs formatting

         @Html.DisplayFor(model => model.B) /// For some reason this does not work

    Wednesday, August 7, 2019 12:08 PM