Reputation: 1035
I have string message that I want to put HTML code into that in controller. how can I do this?
string message = @"<div style="direction: ltr; width: 700px; margin: 0px auto; font-family:Verdana; font-size:11px; line-height:25px;">
<div style="line-height: 21px; color: rgb(68, 68, 68); font-family: Calibri, sans-serif; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);">
<div style="line-height: 21px; min-height: 14px;">
<br class="Apple-interchange-newline" />
<br style="line-height: 21px;" />
</div>
</div>";
but it shows error. how can I do this?
Upvotes: 0
Views: 9859
Reputation: 2793
There Are Many Ways to Do That ..
The way you are doing in asp.net mvc is is not best practice as it mixes your view(presentation) in your controller..
To Do That :
1. use TagBuilder and create Each tag and add attributes ..
2. use StringBuilder and use AppendFormat and AppendLine To Format String
and use code variables inside your HTML Markup
3. return some name from controller and control from javascript from
this name the html you have to render (you can use Jquery templates)
use MVCHtmlString to create html
Upvotes: 1
Reputation: 33305
Looking at your code I would take the following approach to avoid mixing your View and code.
I would create a partial view that contains your html code and load it from the view as
@Html.Partial("~/Views/Shared/_html.cshtml");
If your html needs to dynamically change I would pass the model into it like this:
@Html.Partial("~/Views/Shared/_html.cshtml", model);
and replace the dynamic elements.
Upvotes: 0
Reputation: 33857
It would probably be preferred if your controller had a model for the html data you want to display and then have it return a partial view that creates your html according to the model. This keeps your html presentation contained in a view, as the framework is designed.
Might also be preferred to have your styling in a CSS file and apply a class to these elements instead - slightly cleaner to update.
Upvotes: 0
Reputation: 1
Best practice with control such that you should not have anything which will directly posted as html. Instead prefer html helper as view and reuse the same when you need them.
Upvotes: 0
Reputation: 10694
You can use MvcHtmlString
to create html elements on controller side
public static MvcHtmlString CreateHTML<TModel, TProperty>(this HtmlHelper<TModel> helper, string yourstring)
{
return MvcHtmlString.Create(htmlString);
}
Or you can use TagBuilder
var builder = new TagBuilder("a");
builder.SetInnerText("Your Text");
builder.MergeAttribute("href", "http://test.com");
builder.MergeAttribute("class", "setPage");
And then appent this bulder.ToString() to stringbuilder
Upvotes: 1
Reputation: 9606
Try to replace double quotes with single quotes, which are in HTML markup.
Upvotes: 1