2

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?

k.c.
  • 1,755
  • 1
  • 29
  • 53

2 Answers2

1

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; }
}
Aelios
  • 11,849
  • 2
  • 36
  • 54
  • The question deals with the CLIENT/BROWSER side part. I'm looking for a javascript function that will take a Html string and will encoded it, so it can be set as the value of a hidden field. On the server we can use Server.HtmlDecode to reproduce the sent HTML that is not the problem – k.c. Aug 12 '14 at 09:40
  • [ValidateInput(false)] works. The [Allow Html] does not, as we use the form collection at various places. This is covered in the following post: http://stackoverflow.com/questions/4861927/allowhtml-attribute-not-working The points are yours. – k.c. Aug 12 '14 at 11:13
0

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)