Steve
Steve

Reputation: 3080

HTML Encoded Json object with Jquery Template in MVC

So I have the ability to post comments on my site. User enters a field hits "post" and i ajax the comment back to the database which is dealt with it in this Action result

public ActionResult PostComment(Comment NewComment)
    {
        var repository = GetRepository<Comment>();
        var player = GetPlayer();
        //we have this stuff
        NewComment.Created = DateTime.Now;
        NewComment.Updated = NewComment.Created;
        NewComment.Live = true;
        NewComment.Player = player;

        repository.Add(NewComment);

        return new JsonResult { Data = new { Success = true, Comment = new CommentSummary(NewComment, player) } };
    }

This is the reuturned to my jquery which uses jquery template to display the comment on the page

 $.post("/comments/PostComment", data, function(json) {
        if(json.Success){              
            var newComment = [
              {
                Id: json.Comment.id,
                postedOn: json.Comment.postedOn,
                postedBy: json.Comment.postedBy,
                body: json.Comment.body
              }
            ];

            /* Compile markup string as a named template */
            $.template("TmplComment", $("#CommentTemplate").html());

            /* Render the named template */
            $.tmpl("TmplComment", newComment).prependTo("#AllComments");

            //Do some tidying
            $('#Comments_HasComments').show();
            $('#Comments_HasNoComments').hide();
            $("#frmPostComment textarea[name='comment']").val('')

            //go to your comment
            $("#Comment_" + json.Comment.id).animate({backgroundColor: "#F4FF8C"}, 1000 ).animate({backgroundColor: "#ffffff"}, 1000 );
            location.href = "#Comment_" + json.Comment.id;
        }                        
    });

for some reason I am having HTML encoded issues as any enters the user has entered will be converted to
when posted back down but the jquery template puts them onto the page in full view instead of being just part of the code

is there something i need to turn on/off??

Upvotes: 0

Views: 923

Answers (1)

RandomEngy
RandomEngy

Reputation: 15413

Are you using the {{html}} template tag for the comment body?

Upvotes: 1

Related Questions