get output of Html.BeginForm() into string - c#

I am trying to get the string output of Html.BeginForm() as a string inside a view, without printing it in the view. Is it possible?
EDIT >
I am trying to implement some text helpers to work with html tags inside C#, such ass:
using(Html.BeginText("p")){
Hi, my name is Ashley. I love
using(Html.BeginText("small")){
small letters
}
}
I used IDisposable, so I don't have to worry about the closing tags and I can insert html text inside the brackets, but I want to get this values as string, not to print it to the user.

Related

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

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.

Extracting HTML tag from a string

I have a string like
"For more information, go to google"
When I directly put this string inside a span in my razor (cshtml) view, I get the raw string in the DOM. How can I make it behave like HTML content, i.e. text followed by a link? Is there any way other than parsing the string and extracting the href and link text and then putting them inside a new anchor tag?

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)

Outputting MVC ViewBag text with some areas not encoded?

Consider the following example:
<div>#ViewBag.MyData</div>
What to do if ViewBag.MyData contains a string with "\n" (newlines) in it, that I want to change into "<br/>"? When I include "<br/>" instead of the newlines, it gets encoded in the browser, which is bad.
How can I help this?
use #Html.Raw(ViewBag.MyData.Replace("\n", "</br>"))
the following will do what you need...
#MvcHtmlString.Create(ViewBag.MyData.Replace("\n", "</br>"))
The Create method will take a HTML string and render it as would be expected if you just typed the HTML straight onto the page
That's what html pre tag is used for
The < pre> tag defines preformatted text. Text in a < pre> element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks ...
And in razor template
< pre>#ViewBag.MyData</ pre>

Categories