Mukesh Kumar
Mukesh Kumar

Reputation: 2376

Partial View is not reloading using Jquery in MVC3

I am created a application in MVC3 and I want to reload the partial view on button click, but partial view is not updating.

ActionResult Method

  public ActionResult CommentForPost(int postId)
    {
        var PostComments = _commentsRepository.GetCommentsByPostID(postId).Select(u => new CommentsViewModel()
       {
           CommentUserFullName = u.CommentUserFullName,
           CommenterEmailId = u.CommenterEmailId,
           CommenterSite = u.CommenterSite,

       }).ToList();

        return PartialView("PostedComments",PostComments);
    }

Partial View inside Main View

<div id="dvPostedComment">
@Html.Partial("PostedComments", Model.Post)
</div>

Jquery

           var postId = $("#hdnPostId").val();

            var d = "postId=" + postId;
            $.ajax({
                type: "POST",
                url: "/Comments/CommentForPost",
                data: d,
                success: function (r) {
                    $('#dvPostedComment').html(data);
                },
                complete: function () {
                    alert('hello2');
                },
                error: function (req, status, error) {
                    alert('helll3');
                }
            });

It always goes in error section.

Anyone can help me.

Upvotes: 0

Views: 253

Answers (2)

Dr. Rahul Jha
Dr. Rahul Jha

Reputation: 1054

Please try like this. I have created a small example which exactly do same which you want to achieve in your code. You can do this in two ways- using $.ajax() or using $.load(). $.load() method do same what you want.

_InnerPartial.cshtml

@model int
@{
     <h1>This is a partial view</h1>
     <h3>Your passed value=@Model</h3>         
}

Controller method

[HttpPost]
    public PartialViewResult CallPartial(int value)
    {
        return PartialView("_InnerPartial", value);
    }

Index.cshtml

@{
ViewBag.Title = "Index";
}

<h2>Index</h2>

<div id="divPartial"></div>

<script>
$(document).ready(function () {
    AsyncAjaxCallback();
    //AsyncCallback();

});
function AsyncCallback() {
    var ctrl = $("#divPartial");
    var url = "/Example/CallPartial";

    if (url && url.length > 0) {
        ctrl.load(url, { value: 45 });
    }

}

function AsyncAjaxCallback() {
    $.ajax({
        type:"POST",
        url: "/Example/CallPartial",
        data: { value: 78 },
        success:function(result)
        {
            debugger;
            $("#divPartial").html(result);

        }
    });
}

</script>

For more details please see this link. http://dotnet-concept.com/Article/2014/8/15/How-to-load-partial-from-jquery-in-MVC

Hope this can help you.

Upvotes: 1

user3263194
user3263194

Reputation: 453

You are trying to load "data" in the partial view div but "data" doesn't exist. The result of ajax call is r as you have mentioned.

So try using

success: function (r) {
                $('#dvPostedComment').html(r);
            },

If you are using $.get then, use the below code:

$.get(url, 
     { "postId": PostId},
      function (data) { $('#dvPostedComment').html(data); 
});

Upvotes: 0

Related Questions