0

I am not sure that even the question is correct, so I am trying to explain what I want to achieve and hope you can help me with the best solution.

I am using in many places in my project the bootstrap modal, so I made an helper to make my life easier.

The helper is working with only one exception, I am not able to give the body content in the correct format.

The helper need 4 parameters to run:

@Html.MyModal(string bodycontent, string title, string closeBtnText, string id)

I have the bodycontent stored in the database like html, and I was planing to use this helper @Html.Raw(bodycontent)like this:

 @Html.MyModal(Html.Raw(model.Content).toString(), model.Title, resources.Close, model.Id)

Obviously is not working, still showing the content with html tags.

Here is the helper code which handle the bodyContent:

var bodyTag = new TagBuilder("div");
    bodyTag.MergeAttribute("class", "modal-body");
    bodyTag.MergeAttribute("style", "overflow-y: scroll;height:250px;");
    bodyTag.SetInnerText(bodycontent);

bodycontent is helper parameter, of type string.

Can you please help me with a solution in order to display the bodycontent like plain text (in database is stored like html)?

Lucian Bumb
  • 2,821
  • 5
  • 26
  • 39

1 Answers1

1

One solution to my problem, is:

  1. I changed the data type for bodycontent from string to IHtmlString
  2. I replace the following line in the helper:

    bodyTag.SetInnerText(bodycontent);

with:

 bodyTag.InnerHtml=bodycontent.ToHtmlString();

and now my helper is working as expected:

 @Html.MyModal(Html.Raw(model.Content), model.Title, resources.Close, model.Id)
Lucian Bumb
  • 2,821
  • 5
  • 26
  • 39
  • Should'nt `@Html.MyModal(model.Content, model.Title, resources.Close, model.Id)` be enough? Bootstrap should handle it automatically? – joym8 Jan 20 '16 at 20:54
  • is not enough, the model.content contain html tags and by using it without Html.Raw(), I was getting the tags into the modal body – Lucian Bumb Jan 20 '16 at 20:56
  • that was my way with modal, but when I try it dynamically with a helper, did not work, only in the way I describe it.because the html content is stored like string in database, and you need something to tell to the browser that is not plain text – Lucian Bumb Jan 20 '16 at 21:13