【问题标题】:HTML Encoded Json object with Jquery Template in MVC在 MVC 中使用 Jquery 模板的 HTML 编码的 Json 对象
【发布时间】:2011-04-05 22:14:30
【问题描述】:

所以我有能力在我的网站上发布 cmets。用户输入一个字段点击“发布”,我将评论返回到数据库,在此操作结果中处理它

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) } };
    }

这是返回给我的 jquery,它使用 jquery 模板在页面上显示评论

 $.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;
        }                        
    });

由于某种原因,我遇到了 HTML 编码问题,因为用户输入的任何输入都将在发布后转换为
,但是 jquery 模板将它们以全视图的形式放在页面上,而不仅仅是代码的一部分

有什么我需要打开/关闭的吗??

【问题讨论】:

    标签: asp.net-mvc templates jquery


    【解决方案1】:

    您是否使用{{html}} template tag 作为评论正文?

    【讨论】:

    • 完美,你让我看到了更多 jquery 模板的东西,谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-11
    相关资源
    最近更新 更多