在返回Partial View的ajax响应中获取模型计数 [英] Get model count in ajax response returning Partial View

查看:71
本文介绍了在返回Partial View的ajax响应中获取模型计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过ajax在Issues控制器中调用名为Issues的方法.

I am calling a method named Issues in Issues controller through ajax.

var url = "@(Url.Action("Issues", "Issues"))";
$.ajax({
    type: 'POST',
    url: url,
    data: issue,
    dataType: "html",
    success: function (evt) {
        $('#filteredDataList').html(evt);
    },
});

在我的控制器中:

[System.Web.Mvc.HttpPost]
public ActionResult Issues(IssuesModel issue)
{
    var model = allIssuesList.OrderBy(p => p.ID).ToList();
    return PartialView("~/Views/Issues/_LoadMoreIssues.cshtml", model.Take(20));
}

我需要的是一种在ajax调用的成功响应中获取模型计数的方法,因此我可以执行以下操作:

What I need is a way to get the model count in the success response of the ajax call, so i can do the following:

success: function (evt) {
    $('#filteredDataList').html(evt);
    var modelCount = ???
    if (modelCount > (20)) {
        $("#loadMore").show().fadeIn(2000);
    }
},...

有没有办法实现这一目标?

Is there any possible way to achieve this?

推荐答案

您没有将模型返回给ajax调用,而是返回了html.

You're not returning a model to the ajax call, you're returning html.

您可以在html中包含所需的任何信息,然后从中读取.

You can include any information you like in the html and read it from there.

首先,添加一个视图模型以包含您的其他信息:

First, add a viewmodel to include your additional information:

 public class MoreIssuesViewModel 
 {
     public IList<Order> Orders { get;set; }
     public int Total { get;set; }
 }

从控制器使用它:

[System.Web.Mvc.HttpPost]
public ActionResult Issues(IssuesModel issue)
{
    var data = allIssuesList.OrderBy(p => p.ID).ToList()
    var model = new MoreIssuesViewModel 
    {
        Orders = data.Take(20),
        Total = Orders.Count()
    }
    return PartialView("~/Views/Issues/_LoadMoreIssues.cshtml", model);
}

然后在您的视图中,将新信息存储在某个地方(示例)

Then in your view, store the new information somewhere (example)

@model MoreIssuesViewModel
<table data-total='@Model.Total'>
  <tbody>    
      @foreach(var order in Model.Orders) {
         <td>....</td>
      }

然后您可以从success

success: function (evt) {
    $('#filteredDataList').html(evt);
    var modelCount = $('#filteredDataList table').data("total")
    if (modelCount > (20)) {
        $("#loadMore").show().fadeIn(2000);
    }
},...

这篇关于在返回Partial View的ajax响应中获取模型计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆