Reputation: 65
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.
Upvotes: 0
Views: 789
Reputation: 1
could you provide more details? Where is your controller running? Is there any specification of your Controller youre posting data to?
In order to get the HTML code of your jQuery div use the .html()
function - link
The post function works as follows:
jQuery.post( url [, data ] [, success ] [, dataType ] )
The second argument must be a string or a plain object. Therefore using the html()-Function should work.
Here is a fiddle I made (basicly a copy pasta of the jsfiddle.net echo demo + jquery post demo): https://jsfiddle.net/vyrdp7uo/
Upvotes: 0
Reputation: 76557
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).
Upvotes: 1
Reputation: 760
On parent element of div, call innerHTML, this will return the HTML as a string, which you can then post.
Upvotes: 0