How to pass a div element as a parameter to an MVC controller?

seeSharpGeek

how can i pass a <div>Hello World</div> using jQuerys $.post() function to an MVC controller like public JsonResult GetDiv(string element) I've tried encodeURL() on the client but I keep getting an Internal Server Error(500)...i'm basically trying to save HTML code to SQL Server. Thanks.

Rion Williams

It's very likely that you'll need to decorate the specific property that you are attempting to pass with the [AllowHtml] attribute as seen below on the property that you are attempting to use HTML for:

public class HtmlContent 
{
    [AllowHtml] 
    public string Content { get; set; }
}

And then simply bind the to that property within your Controller Action:

public JsonResult GetDiv(HtmlContent element) 
{ 
     // Access your element here
     var html = element.Content;

     // Save to the database
}

This will let .NET know that this string is expected to contain HTML content, which it may otherwise see as potentially malicious content and reject (similar to the 500 error that you are experiencing).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to pass @RequestBody parameter of controller using MockMVC

How to pass parameter to FormType constructor from controller

How to pass a null value for a URL parameter to Spring MVC controller?

Pass parameter to controller from @Html.ActionLink MVC 4

how to pass parameter with @url.action to controller

How to pass nested list element to the controller in spring mvc

How to pass <div> value to another Controller in ASP.Net MVC?

How to pass parameter to controller function

How to pass a parameter to a Mobx Controller (Flutter)

ASP.NET MVC How to pass JSON object from View to Controller as Parameter

How to pass a parameter in place of controller name in ASP MVC?

Pass Servlet Context Parameter to a Spring MVC Controller

asp.net mvc, how to pass actionlink parameter from form element

How to pass a serializeArray as json to MVC controller

How to pass a parameter (that identify the primery key of an object) from the view to a controller using Spring MVC?

How to pass string value of MVC DropDownList as a parameter to method in controller?

How to pass List<model> to controller in MVC 4

How to pass parameter from ActionLink to controller at runtime

MVC C# Pass parameter to VIA ActionLink to Controller

How to pass two forms as parameter to laravel controller?

How to pass a object parameter to the controller Spring mvc by an onclick

How could I pass multiple collections to View as parameter from Controller in ASP.NET MVC?

how to pass two parameter in route controller

How to pass output parameter in a controller in an WebAPI

how to pass model as parameter to controller

How to pass local storage item to MVC controller

NodeJS - How to pass url parameter to controller function?

How to pass parameter using JavaScript to MVC Controller - ASP.NET 7

How to pass correctly parameter from view to controller?