Instead, you should use IHtmlString :
IHtmlString str = new HtmlString("<a href="/Home/Profile/seeker">seeker</a> has applied to <a href="/Jobs/Details/9">Job</a> floated by you.</br>");
Whenever you have model properties or variables that must contain HTML, I find that this is usually a better practice. First of all, it's a little cleaner. For example:
@Html.Raw(str)
Compared with:
@str
In addition, I also think this is a little safer than using @Html.Raw() , since the problem is whether your data is stored in HTML in your controller. In an environment where you have front-end and third-party developers, your third-party developers may be more accurately what data may contain HTML values, thereby saving this problem in the background (controller).
I usually try to avoid using Html.Raw() whenever possible.
Another thing worth noting, I'm not sure where you assign str , but a few things that concern me with how you can implement this.
Firstly, this should be done in the controller, regardless of your solution ( IHtmlString or Html.Raw ). You should avoid logic like this, in your opinion, since it does not really belong.
In addition, you should use the ViewModel to get the values โโin your view (and again, ideally using IHtmlString as the property type). Seeing something like @Html.Encode(str) bit relative if you are not doing this just to simplify your example.
Jerad Rose Nov 14 '13 at 14:51 2013-11-14 14:51
source share