0

我正在开发 MVC 3 应用程序并使用剃刀语法。

在这个应用程序中,我提供评论功能。

我在每个 Div 中添加了评论链接。. 当用户单击该评论链接时,它会加载包含用于添加评论的控件组的部分视图。

现在我的问题是关于删除新评论。

我有删除已保存评论的代码..它工作得很好......

现在的问题是当用户输入新评论并尝试删除时,它不会被删除......

见蓝色方块。

看这张图就明白了……

在此处输入图像描述

我的代码是...

<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>

@model  IEnumerable<CRMEntities.Comment>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>



//Button which Saves currently added comment in DB as well display on screen...
<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#AddCommentButton').click(function () 

        {     

       // alert("clicked");
            $.ajax({

                type: 'post',
                url: '/Comment/SaveComments',
                dataType: 'json',
                data:
                { 

                 'comments' : $('#Comment').val(), 
                 'EType' : @Html.Raw(Json.Encode(ViewBag.EType)), 
                  'EId' : @Html.Raw(Json.Encode(ViewBag.EId))

                },
                success: function (data) {

                    $("p.p12").append('<div style="background-color:#FAFAFA;">Recently Added... <br /><a href="@Url.Action("Details", "Employee", new { id = "__id__" })'.replace('__id__', data.OwnerID) + '">' + data.OwnerName + '</a>'+ data.cmtDateTime +'<button type="button" id=' + data.Id  + ' class="deleteComment">Delete</button></span><br />' + data.msg + '</div>')


                }

            });
        });
    });
</script>





<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $(".ShowComments").click(function () {
            $(".ParentBlock").slideToggle("slow");
     });
    });
</script>


</head>
<body>

@{



     <div class="ParentBlock">


    @foreach (var item in Model)
    {
        <div class="OwnerClass" id="OwnerName">


         <span class="EmpName"> @Html.ActionLink(item.Owner.FullName, "Details", "EMployee", new { id = item.OwnerId }, new { @style = "color:#1A6690;" })</span>

           @Html.DisplayFor(ModelItem => item.CommentDateTime)

          <span class="EmpName"><button type="button" id = "@item.Id" class="deleteComment">Delete</button></span>



        <p class="CommentP">
           @Html.DisplayFor(ModelItem => item.CommentText)
        </p>


        </div>


    }


     <p class="p12">

      </p>



</div>

      <p id="ClassPara" class="ShowComments" onclick="chkToggle()">Show All Comments</p>

}



   @Html.TextArea("Comment", "", 5, 80, "asdsd")


    <input type="button" value="Add Comment" id="AddCommentButton"/>                         
    <input type="button" value="Clear" onclick="clearText()"/>                    

    <br />







</body>
</html>





<script src="../../Scripts/jquery.js" type="text/javascript"></script>
<script type="text/javascript">
//   Working code - Deletes the comment from DB and removes hide the current Div
////////////////////////////////////////////////////////

    $(document).ready(function () {
        $(".deleteComment").click(function ()
         {
            alert("asd");

            var Id = $(this).attr("id");
           var self = this;

            var url1="@Html.Raw(Url.Action("DeleteComment", "Comment", new { id = "idValue" }))";
            url1=url1.replace("idValue",Id );
            alert(url1);

            $.ajax(
            {

                type: 'post',
                url: '/Comment/DeleteComment',
                dataType: 'json',
                data:
                { 

                 'EId' : Id

                },
                success: function (data) 
                {
                alert ("Hello");



                    $(self).closest("div").hide("slow");
                }

            });

        });
    });



</script>
4

1 回答 1

0

在追加新 div 时的成功方法中,只需向删除按钮添加一个类,其 CSS 为display:none

例如:

$("p.p12").append('<div style="background-color:#FAFAFA;">Recently Added... <br /><a href="@Url.Action("Details", "Employee", new { id = "__id__" })'.replace('__id__', data.OwnerID) + '">' + data.OwnerName + '</a>'+ data.cmtDateTime +'<button type="button" id=' + data.Id  + ' class="deleteComment hidden">Delete</button></span><br />' + data.msg + '</div>')

CSS:

.hidden { display: none;}

当你准备好展示它时,你可以简单地从按钮中删除类。

于 2012-09-22T13:21:41.167 回答