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?

7
  • why don't you just save html in database??? Commented Aug 12, 2014 at 7:52
  • @Exception: That is exactly what we want to do, but first it has to go from the client machine to the server in the form post Commented Aug 12, 2014 at 7:54
  • yes..so..you can save exact html in database from client machine to database... Commented Aug 12, 2014 at 7:55
  • send it as plain string only.. Commented Aug 12, 2014 at 7:56
  • you can post, but what is your exact problem, if it is not reaching to your controller then you have to palce [AllowHtml] attribute to your controller method Commented Aug 12, 2014 at 7:56

2 Answers 2

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; }
}
Sign up to request clarification or add additional context in comments.

2 Comments

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
[ValidateInput(false)] works. The [Allow Html] does not, as we use the form collection at various places. This is covered in the following post: stackoverflow.com/questions/4861927/… The points are yours.
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)

1 Comment

this should be comment. write your response either in your replied answer or comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.