Using ASP.NET Razor not MVC3 how do I allow html to render in a post? What method? - razor

I am familiar with the [AllowHtml] parameter in MVC3, but I am building a site based on Asp.net and Razor not inline with MVC3. How do I allow users to enter html in their post and not get that error message? What property or setting do I need to make to allow the html to pass through? I am using LINQ to talk to the database and the property I would like to allow html for is the Body property.
var Body = Request["Body"];

Like this...
Request.Unvalidated("Body")

Related

How to render a view to html string in asp.net core controller?

In my asp.net core 2.0 application, I need to show the details of a payment receipt as a view to the user. I also need to send to user the same html text as an email (minus the top navigation bar).
My thought was to put the payment details in a partial view. I can then use this partial view on the website as well as in the email.
In appears Html object is injected into razor pages automatically such that one can, for example, call #Html.Partial(). Is there a way to use this Html object from the Controller class? Or, is there a different mechanism to render .cshtml file into html string? Regards.
You might try Microsoft.AspNetCore.Html.HtmlString or Microsoft.AspNetCore.Html.HtmlFormattableString. You can return these directly from the controller.

What Is Done When Spring Form Tags Are Limited

What happens when using SpringMVC and the Spring Tag library does not have the tags you require? What can the developer use that will allow automated databinding, does the developer now have to use the old way of doing databinding which involves using the http *request*, response to get and set form elements?
Can the developer add to the Spring-Form tags? I am confused on how to approach this since i am using tags form html5 and they do not exist in the Spring Tag library.
Can someone advise me on the best approach to handling a problem such as this. I have been asking questions on how does one use a **<canvas>** tag in SpringMVC so that it allows automated databinding like spring <form> tags.
You can still get the binding done by providing the name attribute value of canvas tag same as the name of the field in your pojo you want to bind the value with.
Eg. if your field name in pojo is myCanvas, then you can give the same name in name attribute of canvas tag. So that the binding gets done.
Hope this helps you. Cheers.

Passing Razor Markup to view and process it

I've searched the internet for days now with no luck finding this.
My model has a property which holds a chunk of html containing Razor markup.
exmaple:
public class ViewModel
{
public string Content = "<div>#Html.TextBox(\"UserName\")</div>";
}
In the view, I display that with
#Html.Raw(Server.HtmlDecode(Model.Content).toString())
I need to be able to convert the Razor markup into html, although because the Content is dropped in through the model, the view engine doesn't process it.
I have tried simply dropping in the Content, using just .Raw(Model.Content), .Encode(Model.Content), nothing works.
Any thoughts?
You could use the RazorEngine package which allows you to parse and execute Razor code. This being said I would not recommend you giving your users the power of editing directly Razor templates. You are opening a huge security hole in your website.
There are other templating engines such as DotLiquid for example which are better suited for scenarios where you don't trust user input.

How to append a custom HTML attribute or CSS class using a .NET data annotation attribute?

I'm using ASP.NET MVC 3 RTM. Is it possible to change the HTML rendered from a View Model, by using an attribute?
Example:
public class Product
{
[AddHtmlAttribute(Name = "disabled", Value = "disabled")]
public string Name { get; set; }
}
I want the attribute to be able to change the rendered HTML, that property results in. I know it properly can't be done with an attribute alone. I probably have to hook into the system by implementing an interface, but where should I look?
I know MVC uses the default editor templates, and I've looked at them in the MVC 3 source code, but I haven't been able to figure out if it would be possible to somehow hook into the rendered element and add some attributes. I know the validation system does this, by adding custom HTML attributes to support unobtrusive validation.
I guess I just need a pointer to where I should look, or what interface I should take a look at.
Thank you so much.
Update: I'm using the standard HTML helper Html.EditorFor(model => model.Name) for my fields, and haven't overriden any editor templates. I would really prefer if I didn't have to change or override the default templates.
You may checkout the following blog post which illustrates how to achieve this by writing a custom DataAnnotationsModelMetadataProvider, attribute and overriding the default templates.

How to display HTML stored in a database from an ASP.NET MVC view?

I have HTML code edited by FCKEditor stored in a database and would like to display (well render) it onto a view. So, for instance, something stored as:
<>pre<>This is some sample text<>pre</&gt
Will be displayed to the user as:
This is some sample text
(With the appropriate style for pre-formatted-text)
The view already has the required string to display from ViewData, I'm just not sure what the best way to show it to the user is.
Try this:
<%= System.Web.HttpUtility.HtmlDecode(yourEncodedHtmlFromYouDatabase) %>
More info here.
The answer provided by Pure.Krome is flawless for MVC2, but consider Razor syntax:
#Html.Raw(System.Web.HttpUtility.HtmlDecode(Model.yourEncodedHtmlFromYourDatabase))
Alternatively,
#Html.Raw(Server.HtmlDecode(Model.yourEncodedHtmlFromYourDatabase))
You want to use #Html.Raw(str).
See MSDN for more information.
Returns markup that is not HTML encoded.
This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML.