Search code examples
asp.net-mvcrazor

Replacing string with HTML control MVC View


<td style="font-size:1em;font-family:Verdana;color:darkkhaki">                            
@item.ObjQuestions.QuestionText.Replace("____", "<input type='Text'></input>")
</td>

I have seen a lot of similar questions, but I guess this is not a duplicate, because all the questions are how to add a control on serve-side. I want to replace a string that I get in my object.property in my cshtml View. I want to inject a textbox (or any html control) in place of some special characters.

The above code snippet simply replaces <input type='Text'></input> as text.

For e.g.

Acronym of CSS is ##

Here ## should be replaced by a textbox.


Solution

  • MVC will by default HTML encode all your strings in your view. You have tell it to output it as a raw HTML string instead by using Html.Raw:

    @Html.Raw(item.ObjQuestions.QuestionText.Replace("____", "<input type='Text'></input>"))