我坚持用Ajax.BeginForm在无限滚动中递增页面变量

时间:2014-04-19 10:52:41

标签: ajax asp.net-mvc asp.net-mvc-5

我在MVC 5中实现无限滚动,通过Ajax.BeginForm调用一个动作返回局部视图,我正在努力增加页面变量,它总是向动作发送1!

控制器

        public ActionResult Index()
        {
        home.Articles = GetArticlesByPage(home.Page);
        return View(home);
        }

    public PartialViewResult NextPageArticles(int page)
        {
        home.Articles = GetArticlesByPage(page);
        return PartialView("~/Views/Home/ArticlePostPartial.cshtml", home.Articles);
        }

HomeViewModel

    public class HomeViewModel
    {
    public IEnumerable<ArticleViewModel> _articles;
    public IEnumerable<ArticleViewModel> Articles
    {
        get { return this._articles; }
        set
        {
            this._articles = value;
            Page++;
        }
    }
    public int Page { get; set; }

    public HomeViewModel()
    {
        Page = 0;
    }
}

查看

<div id="MainContainer" class="container-fluid">
      @{Html.RenderPartial("ArticlePostPartial", Model.Articles);}
</div>
@using (Ajax.BeginForm("NextPageArticles", "Home", new { page = ??? }, new AjaxOptions
    {
        InsertionMode = InsertionMode.InsertAfter,
        UpdateTargetId = "MainContainer",
        LoadingElementId = "imgload",
        OnSuccess = "OnSuccessAjax"
    }))
{
    <input type="submit" id="formbut" class="btn btn-danger hidden" />
}

1 个答案:

答案 0 :(得分:0)

这样的事情对你有用。

<div id="MainContainer" class="container-fluid">
      @{Html.RenderPartial("ArticlePostPartial", Model.Articles);}
</div>
@using (Ajax.BeginForm("NextPageArticles", "Home", new { page = Convert.ToInt32(Request.RequestContext.RouteData.Values["page"]) + 1 }, new AjaxOptions
    {
        InsertionMode = InsertionMode.InsertAfter,
        UpdateTargetId = "MainContainer",
        LoadingElementId = "imgload",
        OnSuccess = "OnSuccessAjax"
    }))
{
    <input type="submit" id="formbut" class="btn btn-danger hidden" />
}