我面临着非常奇怪的情况。我有一个客户列表,并根据我筛选该列表的一些条件。例如,如果用户搜索电子邮件我将其过滤掉以获取与特定电子邮件匹配的记录。我也正在计算分页,这在第一页的情况下效果很好。但是当我试着给它2页时,它给了我奇怪的行为。首先看看下面的代码。
对于第1页,这行代码效果很好
int total = _customerlist.Count();
但是当我在参数中有第二页时它会给我“索引超出范围。必须是非负数且小于集合的大小。”错误。但是我仍然记录了第1页中的10条记录和第2页中的5条记录。所以当我收到此错误时,我在“_customerlist”中有5条记录 当我有第1页时,为什么我没有收到此错误?
控制器
public ActionResult CustomerList(int page, int rp, string sortname, string sortorder, string qtype, string query)
{
var _customerlist = _customerRepository.Table;
if (!string.IsNullOrEmpty(qtype) && !string.IsNullOrEmpty(query))
{
if (qtype == "Email")
{
_customerlist = _customerlist.Where(p => p.Email.Contains(query));
}
else if (qtype == "LastName")
{
_customerlist = _customerlist.Where(p => p.LastName.Contains(query));
}
else if (qtype == "FirstName")
{
_customerlist = _customerlist.Where(p => p.FirstName.Contains(query));
}
else if (qtype == "Age")
{
_customerlist = _customerlist.Where(p => p.Age.ToString().Contains(query));
}
else if (qtype == "Sex")
{
_customerlist = _customerlist.Where(p => p.Sex.ToString().Contains(query));
}
else if (qtype == "Email")
{
_customerlist = _customerlist.Where(p => p.Email.ToString().Contains(query));
}
else if (qtype == "MembershipType")
{
_customerlist = _customerlist.Where(p => p.MembershipType.ToString().Contains(query));
}
}
_customerlist = _customerlist.Skip((page - 1) * rp).Take(rp);
int total = _customerlist.Count();
return CreateFlexiJson(_customerlist, page, total);
}
private JsonResult CreateFlexiJson(IEnumerable<CustomerPartRecord> items, int page, int total)
{
List<Object> rows = new List<Object>();
foreach (var item in items)
{
var familystring = string.Empty;
if (item.MembershipType == "Family")
{
var familymembers = from p in _familyRepository.Table.Where(p => p.UserId == item.Email) select p;
int i = 1;
foreach (var familyitem in familymembers)
{
if (i == 1)
{
familystring = familystring + "<label style=\"" + "color:#2B3856;font-weight: bold;" + "\">" + item.FirstName + " " + item.LastName + " (Head)</label>";
familystring = familystring + "<hr>" + "</hr>";
familystring = familystring + "<label style=\"" + "color:#2B3856;" + "\">" + familyitem.FirstName + " " + familyitem.LastName + "</label>";
}
else
{
familystring = familystring + "<label style=\"" + "color:#2B3856;" + "\">" + familyitem.FirstName + " " + familyitem.LastName + "</label>";
}
i++;
}
}
else
{
familystring = "N/A";
}
rows.Add(new
{
id = item.Id,
cell = new string[] {
item.Id.ToString(),
item.FirstName,
item.LastName,
item.Email,
item.HouseNameOrNumber,
item.Street,
item.County,
item.Town,
item.Age.ToString(),
item.Sex,
item.BCUMembershipNumber,
item.MembershipType,
item.PostCode,
item.HomePhone,
item.MobilePhone,
item.DOB,
item.EmergencyContactName,
item.EmergencyContactAddress,
item.EmergencyContactPhone,
item.EmergencyContactMobile,
item.EmergencyContactEmail,
item.CreatedUtc.ToShortDateString(),
String.Format(familystring),
String.Format("<a href=" + "'" + "Member/More?UserId=" + item.Email + "'" + ">View</a>") ,
}
});
}
var result = new { page = page, total = total, rows = rows };
return Json(result);
}
CSHTML
$('#CustomerList').flexigrid({
url: '@Url.Action("CustomerList", "Customer")',
dataType: 'json',
colModel: [
{
display: 'Id',
name: 'Id',
width: 150,
sortable: true,
align: 'left',
hide: true
},
{
display: 'First Name',
name: 'FirstName',
width: 80,
sortable: true,
align: 'left'
},
{
display: 'Last Name',
name: 'LastName',
width: 80,
sortable: true,
align: 'left'
},
{
display: 'Email',
name: 'Email',
width: 140,
sortable: true,
align: 'left'
},
{
display: 'HouseNameOrNumber',
name: 'HouseNameOrNumber',
width: 200,
sortable: true,
align: 'left',
hide: true
},
{
display: 'Street',
name: 'Street',
width: 200,
sortable: true,
align: 'left',
hide: true
},
{
display: 'County',
name: 'County',
width: 200,
sortable: true,
align: 'left',
hide: true
},
{
display: 'Town',
name: 'Town',
width: 200,
sortable: true,
hide: true,
align: 'left'
},
{
display: 'Age',
name: 'Age',
width: 30,
sortable: true,
align: 'left'
},
{
display: 'Sex',
name: 'Sex',
width: 30,
sortable: true,
align: 'left'
},
{
display: 'B.C.U',
name: 'BCUMembershipNumber',
width: 60,
sortable: true,
align: 'left'
},
{
display: 'Membership Type',
name: 'MembershipType',
width: 100,
sortable: true,
align: 'left'
},
{
display: 'Post Code',
name: 'PostCode',
width: 100,
sortable: true,
align: 'left',
hide: true,
},
{
display: 'Home Phone',
name: 'HomePhone',
width: 100,
sortable: true,
align: 'left',
hide: true,
},
{
display: 'Mobile Phone',
name: 'MobilePhone',
width: 100,
sortable: true,
align: 'left',
hide: true,
},
{
display: 'DOB',
name: 'DOB',
width: 100,
sortable: true,
align: 'left',
hide:true
},
{
display: 'EmergencyContactName',
name: 'EmergencyContactName',
width: 100,
sortable: true,
align: 'left',
hide:true
},
{
display: 'EmergencyContactAddress',
name: 'EmergencyContactAddress',
width: 100,
sortable: true,
align: 'left',
hide:true
},
{
display: 'EmergencyContactPhone',
name: 'EmergencyContactPhone',
width: 100,
sortable: true,
align: 'left',
hide:true
},
{
display: 'EmergencyContactMobile',
name: 'EmergencyContactMobile',
width: 100,
sortable: true,
align: 'left',
hide:true
},
{
display: 'EmergencyContactEmail',
name: 'EmergencyContactEmail',
width: 100,
sortable: true,
align: 'left',
hide:true
},
{
display: 'Member Since',
name: 'CreatedUtc',
width: 70,
sortable: true,
align: 'left',
},
{
display: 'Family',
name: '',
width: 150,
sortable: true,
align: 'left'
},
{
display: 'Other Info.',
name: '',
width: 50,
sortable: true,
align: 'left'
}
],
buttons: [
{
name: 'Edit',
bclass: 'edit',
onpress: test
}, {
name: 'Delete',
bclass: 'delete',
onpress: test
}, {
separator: true
}],
searchitems: [
{
display: 'First Name',
name: 'FirstName'
},
{
display: 'Last Name',
name: 'LastName'
},
{
display: 'Email',
name: 'Email'
},
{
display: 'Age',
name: 'Age'
},
{
display: 'Sex',
name: 'Sex'
},
{
display: 'Membership Type',
name: 'MembershipType'
}
],
sortname: "LastName",
sortorder: "asc",
usepager: true,
title: 'Customer',
useRp: true,
rp: 10,
showTableToggleBtn: true,
width: 920,
height: 500,
singleSelect: true
});
答案 0 :(得分:-1)
尝试使用Dynamic Linq