How do I render MVC code in a string on my page? - c#

I have an HTML string that also contains a call to a partial view
When I render the string as such, it renders the HTML but not the Partial View.
My Partial file has the text Welcome to my Partial!
#Model.MyText = "This is my Text #Html.Partial('Partials/Partial') Done!";
#Html.Raw(Model.MyText)
Renders:
This is my Text #Html.Partial('Partials/Partial') Done!
Instead of:
This is my Text. Welcome to my Partial! Done!
How would I achieve this?

The razor snippet only works in the context of the .csharp file itself, not the rendered webpage. When you call #Html.Raw(Model.MyText), it correctly renders the string as html. However the resulting html is not parsed as C# code, it is just a string of text. This is not the way to achieve your desired result.
Everything you need done using C# must be done before the render. After rendering, its just html code/text.

Related

Render Partial View from HTML String using HttpUtility.HtmlDecode or MvcHtmlString.Create

I am trying to render Partial View at the time of rendering HTML String. I am using KENDO UI Editor from where I can insert Code Snippets like this
#(Html.Kendo().Editor()
.Tools(tools => tools
.Clear()
.Snippets(snippets => snippets
.Add("Institute List", "#Html.Action('CourseTags', 'Services')")
)
This will create an HTMLSTRING which can be rendered using
#MvcHtmlString.Create(HttpUtility.HtmlDecode(HTMLSTRING))
But it does not execute this #Html.Action('CourseTags', 'Services') and it simply writes as it is.
is there anything I am missing or doing wrong?

Render Html code with razor

I've searched over this website and I've looked over similar questions and i did not find the answer, I am sure it is somewhere but i did not find it, I have a string like this one for example :
string inputText = "<strong>Hello World</strong>"
This string comes from a certain request in control, and i have no power to change the model or the control. I can only change my razor view, using Html.Raw displays this result :
<strong>Hello World</strong>
And the result i want to be displayed is this one :
Hello World
How is it possible ?
PS: this is only a simple example, it can be any HTML Code.
You should use:
#Html.Raw(HttpUtility.HtmlDecode(inputText))
Decode and then render in html
To render any string (which includes HTML tags) -received from the model- as HTML, use:
#Html.Raw(Model.SomeString)

Why has Html.DisplayTextFor stopped showing the text as HTML?

I have an ASP.NET MVC3 web page which used to nicely display a C# string of HTML nicely. My server code programmatically loads the string with HTML tags for a nice table. The following cshtml has been showing this nice table as a nice table for years:
<h3>Nice Table:</h3>
#Html.DisplayTextFor( model => model.sNiceTable )
But today we notice browsers (Firefox & IE) are instead displaying it as a pile of text showing the HTML tag text:
<table><tr><th>...
rather than rendering it visually as a table, the way it did for years until now. The "Nice Table" header still displays properly.
Looking at the text in Firefox's developer Inspector shows HTML that looks like it should render as HTML, but (after showing the <h3> tag in color) it too has those tags from my string in black, even though when I paste the same string into an HTML file and load it, it shows the nice table. That is, Inspector shows the code as if it should work, but my string is in black for some reason that doesn't show in its code view.
Trying to use "view source" in Firefox or IE seems to be unusable for this, because the view part doesn't even show up there for some reason.
I see there are various other methods such as Display() and DisplayFor(), but they seem to do the same thing.
I haven't really touched this page since it last worked. The only thing I suspect that changed was applying some MS updates that had some other annoying side-effects on my projects.
What can I do to get this text to render again, rather than spew the HTML tags in the string as text?
Edit: model.sNiceTable is a C# string data member of the viewmodel, where the interface is:
public interface IMemberStatsViewModel
{
string sNiceTable { get; set; }
// other properties...
}
Its value gets assembled in my code which is called in the view model's constructor:
public MemberStatsViewModel()
: base(new DataLayer.DataLayer(), ClientInterfaceTypeEnum.Web)
{
InitializeMe();
}
InitializeMe() calls a data layer function which returns the C# string and assigns it to sNiceTable. The string is built based on stuff I look up in the data layer.
You can use Html.Raw
<h3>Nice Table:</h3>
#Html.Raw(model.sNiceTable)

format string containing html

I have a simple string variable that contains a portion of HTML inside. For example:
string contents = "<div><p>Hi how are you. Click here if you want to know more";
I want to include this HTML in page:
<div class="description">
#contents
</div>
However, it messes up the rest of the page because of unclosed tags.
Is there a function (or a helper) that reads and formats the HTML inside for example, to complete the HTML without errors:
#Html.DisplayProperHTML(contents)
This will render as:
<div><p>Hi how are you. Click here if you want to know more</p></div>
There is no such functionality built-in.
You can use the HTML Agility Pack to parse and fix broken HTML.

ASP.NET Which HTML editor can do everything I want?

I have tried to use the standard AJAX HTMLeditor from here (http://www.asp.net/ajaxlibrary/act.ashx) and I have try to work with the FCKEditor (from http://ckeditor.com/)
But both don't do everything. I call the AJAX standard control A and the FCKeditor F.
With the A editor it is impossible to get your HTML text in the HTML content. You can only get it in the Design content. (this next code doesn't do the job: string htmlContentStr = Editor1.Content).
With F it is possible to get it in the HTML content (it does this by default), but to get your changes back in HTML is impossible. (this next code doesn't do the job: string htmlContentStr = FCKeditor1.Value).
So what I need is a HTML editor that is possible to put HTML text in HTML content, a user can make changes in the designcontent and after the changes 're make it must be possible to get the HTMLcontent and put it away in a string or database.
Is this possible or do I need a commercial one to get this feature?
If my question isn't clear, please let me know.
Thnx
I've used XStandard quite easily and it let me manipulate the HTML. I didn't bother using it as a control, but just read and wrote (escaped) the HTML where needed into the asp output.

Categories