I have a variable with html text and model values
var text = "<h1> hello @Model.firstName @Model.lastName </h1>"
and in cshtml I have
@Html.Raw(text)
my code looks like
@model TextModel
@{
var text = Viewbag.text
}
...
<div>
@Html.Raw(text)
</div>
what it renders is "hello @model.firstName @model.lastName"
but what I want is "hello Jack James"
I don’t know what can I do ?
3
Answers
You could do this:
and then:
@Html.Raw(text)
Edited: Not sure what your CSHTML looks like, but it should work (providing it looks something like this):
You only need to use
@Model
to replace@model
:Update:
Since you are using ViewBag in backend,here is a demo:
Model:
C# code:
I would recommend manipulating the string before you pass it to the view.
Controller:
View: