user1668543
user1668543

Reputation: 309

How to hide div in the view of MVC?

I am developing MVC application and using razor syntax.

In this application I am giving comment facility.

I have added a partial view, which loads the comment/Records from DB.

In below image, we can see the comment box which is called run-time for employee index view.

Now I want to remove the div when user clicks on delete button.

But its not working right now...

Please check image...

enter image description here

I have below code in my application...

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

@model  IEnumerable<CRMEntities.Comment>

  <div class="ParentBlock">



    @foreach (var item in Model)
    {
        <div class="OwnerClass" id="OwnerName" data-comment-id="@item.Id">


         <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" class="deleteComment">Delete</button></span>

          <span class="EmpName"> @Html.ActionLink("Delete", "Delete", "Comment", new { id = item.Id }, new { @style = "color:#1A6690;" })</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">
    $(document).ready(function () {
        $(".deleteComment").click(function () {
            alert("asd");
            var commentBlock = $(this).parent('.OwnerClass');
            commentBlock.hide('slow')

     });
    });



</script>

Update Script ....

$(document).ready(function () {
    $(".deleteComment").click(function () {

        var commentBlock = $(this).parent('.OwnerClass');

        $.ajax({
            type: 'post',
            url: '/Comment/DeleteComment',
            dataType: 'json',
            data:
            { 

            ////////////////////////////////////////
            //What should I write here ? How can I get comment ID here ? 
            ////////////////////////////////////////
            },
            success: function (data) {

              var commentBlock = $(this).closest('div');
            commentBlock.hide('slow');

            }

        });

 });
});

Upvotes: 0

Views: 3255

Answers (1)

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16134

Try this:

<script type="text/javascript">
    $(document).ready(function () {
        $(".deleteComment").click(function () {
            alert("asd");
            var commentBlock = $(this).closest('div');
            commentBlock.hide('slow');
     });
    });
</script>

Also, you can use:

<script type="text/javascript">
        $(document).ready(function () {
            $(".deleteComment").click(function () {
                alert("asd");
                var commentBlock = $(this).closest('div');
                commentBlock.html('');
         });
        });
    </script>

Upvotes: 1

Related Questions