Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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>
like image 951
mishap Avatar asked Dec 28 '11 18:12

mishap


1 Answers

Fixed:

In action :

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

        return **PartialView**("Counts", poll);
    }
like image 80
mishap Avatar answered Oct 12 '22 20:10

mishap