k.c.
k.c.

Reputation: 1835

Send HTML string with MVC Form Post

We have MVC an application. we want to offer our customers to enter html texts for use in document generation. For this purpose we have included CK Editor. CK has a function to return the html. We want to 'encode' this Html, so we can place it in an input field that will be included in a form post.

What JavaScript function(s) can we use to convert the Html string to a format that we can send with the formpost. The form post is done via AJAX.


Update: In an other post we found this function:

function htmlEscape(str) {
    return String(str)
        .replace(/&/g, '&')
        .replace(/"/g, '"')
        .replace(/'/g, ''')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');
 }

It looks like this will do the trick, but are there any build-in or JQuery functions that will do this?

Upvotes: 2

Views: 2955

Answers (2)

Henry Chettiar
Henry Chettiar

Reputation: 61

You can give validate input(false) for your post method to allow ck editor content in the class object:

[HttpPost]   
[ValidateInput(false)]  

public ActionResult SaveArticle(ArticleModel model)
{
    return view();
}

In M V C 3 you can also define your model property with html content as [Allow Html]

public class Article Model 
{
    [AllowHtml]

    public string Some Property { get; set; }

    public string Some Other Property { get; set; }
}

Upvotes: 1

Henry Chettiar
Henry Chettiar

Reputation: 61

Why do you want to do it the long cut way by manually replacing the special characters and pass it in a hidden field?.. You can directly post the html content with your property on normal submit button by declaring the post method with validateinput(false)

Upvotes: 0

Related Questions