mishap
mishap

Reputation: 8505

MVC Ajax update partial view

I want to update the partial view every time the ActionLink is clicked. I'm passing the same model to the partial view as the main view. The problem is that partial view is not getting updated. Not sure if I'm on right track.

View :

@model MyPoll.Models.Poll

@Ajax.ActionLink("For", "AddPositive", new RouteValueDictionary { {"id", Model.Id }},new AjaxOptions() { UpdateTargetId = "countsDiv" })

<div id="countsDiv">
    @Html.Partial("Counts", Model)
</div>

Partial:

@model MyPoll.Models.Poll

Positive count : @Model.PositiveCount
Negative count : @Model.NegativeCount

Controller action :

public ActionResult AddPositive(int id)
{
    Poll poll = db.Polls.Find(id);
    poll.PositiveCount++;
    db.SaveChanges();

    return View(poll);
}

The scripts are referenced as well:

 <script src="/Scripts/jquery.unobtrusive-ajax.min.js" type="text/javascript"></script>
 <script src="/Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
 <script src="/Scripts/MicrosoftAjax.js" type="text/javascript"></script>
 <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>

Upvotes: 6

Views: 7164

Answers (2)

mishap
mishap

Reputation: 8505

Fixed:

In action :

public **PartialViewResult** AddPositive(int id)
    {
        Poll poll = db.Polls.Find(id);
        poll.PositiveCount++;
        db.SaveChanges();

        return **PartialView**("Counts", poll);
    }

Upvotes: 6

Greg Bair
Greg Bair

Reputation: 669

It's probably because you are loading jquery last. The other js depends on jquery.

Upvotes: 0

Related Questions