Robotronx
Robotronx

Reputation: 1818

.NET MVC AJAX action method fires multiple times

This action method renders a partial view, which is embedded in another view.

public ViewResult List(int page = 1)
    {
        int totalComments;
        ProductListViewModel prodViewModel = new ProductListViewModel
        {
            CommentsPaginated = repository.CommentsPaginated(page, PageSize, out totalComments),
            PagingInfo = new PagingInfo
            {
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = totalComments
            }
        };
        ViewBag.Page = page;
        return View(prodViewModel);
    }

The List view:

@model ProductWindow.WebUI.Models.ProductListViewModel

@{
    ViewBag.Title = "List";
}

<br />  
<h2>List Products</h2>
@foreach (var m in Model.CommentsPaginated)
{
    <div class="item">
        @m.NR, @m.TXT, @m.ENABLED
            </div>    
} 

The enclosing Index view:

@model string

@{
    ViewBag.Title = "Index";
    AjaxOptions ajaxOpts = new AjaxOptions
    {
        UpdateTargetId = "commentsData"
    };
}

<h2>Index</h2>
Static part of the page.
<h2>Index page</h2>    
@using (Ajax.BeginForm("List", ajaxOpts)) {  
    <div id="commentsData">
        @Html.Action("List", new {page = Model});  
    </div>       
<input type="hidden" name="page" value="3" />
<input type="submit" value="Submit" />     <!-- I use Ajax.ActionLink here, but it acts the same -->
}

QUESTION:

Whenever I click on Submit, List action method gets fired more and more times (1, 2, 4, 8, 16...). Any idea why?

Upvotes: 0

Views: 933

Answers (1)

Robotronx
Robotronx

Reputation: 1818

Solved it. Changed the List return value to PartialViewResult, and called PartialView() at the end of the same function.

Upvotes: 2

Related Questions