ASP.Net Bootgrid 集成(无排序)
ASP.Net Bootgrid Integration (without sorting)
几天来我一直在努力尝试使用我的 ASP.Net 应用程序实施 jQuery Bootgrid。到目前为止,这就是我所拥有的:(Order By Functionality 还没有工作,我稍后会解决)
public JsonResult IndexJson(RequestData model)
{
var result = (from x in db.ContactSet
select new
{
x.AccountId,
x.FirstName,
x.LastName,
x.FullName,
x.JobTitle,
x.ParentCustomerId,
x.EMailAddress1,
x.Telephone1,
x.MobilePhone,
x.Fax,
x.GenderCode,
x.BirthDate
}); //? Gets all rows
result = (from x in result
where x.FirstName.Contains(model.searchPhrase)
|| x.LastName.Contains(model.searchPhrase)
select x); //? Search Filter
var totalRows = result.Count(); //? Sets totalRows (for ResponseData)
if (model.rowCount == -1)
model.rowCount = totalRows; //? In case View All Rows is selected by Bootgrid (for ResponseData)
// TODO: Add Order By functionality
var tResult = new ResponseData<object>()
{
current = model.current,
rowCount = model.rowCount,
rows = result.ToList(),
total = totalRows
}; //? Builds Json Response
return Json(tResult, JsonRequestBehavior.AllowGet);
}
此代码的问题是我需要在搜索功能后计算记录总数,而我只是不太擅长正确使用 LINQ 查询。
当我到达 var totalRows = result.Count();
时,我收到以下错误:
System.NotSupportedException: 'The method 'Where' cannot follow the method 'Select' or is not supported. Try writing the query in terms of supported methods or call the 'AsEnumerable' or 'ToList' method before calling unsupported methods.'
知道这里出了什么问题吗?
我一直在不同的情况下使用 bootgrid,包括实现服务器端分页和排序 asc,desc 没有任何问题。
试试这个:
//Let's assume this is your model....
public class RequestData
{
public int RowCount { get; set; }
public int Current { get; set; }
public string Search { get; set; }
public string SortBy { get; set; }
public string SortDirection { get; set; }
public int TotalItems { get; set; }
}
1.If 您没有选择数据库的所有列 table,请创建一个 DTO 来映射您选择的列。否则跳过这部分并将 ContactSetDTO 替换为 ContactSet
public class ContactSetDTO
{
public string AccountId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string JobTitle { get; set; }
public string ParentCustomerId { get; set; }
public string EMailAddress1 { get; set; }
public string Telephone1 { get; set; }
public string MobilePhone { get; set; }
public string Fax { get; set; }
public string GenderCode { get; set; }
public DateTime BirthDate { get; set; }
}
2.Assuming您正在使用SQL服务器,您可以使用以下方法获取计数:
public int getContactSetCount(string searchPhrase)
{
int ret = 0;
try
{
string query = string.Empty;
if (!string.IsNullOrWhiteSpace(searchPhrase))
{
// ********* Assuming your db table is also called ContactSet **********************
query = @"SELECT COUNT(*) FROM ContactSet s WHERE s.FirstName LIKE '%' + @p0 + '%' OR s.LastName LIKE '%' + @p0 + '%')";
ret = db.Database.SqlQuery<int>(query, new System.Data.SqlClient.SqlParameter(parameterName: "@p0", value: searchPhrase)).FirstOrDefault();
}
else
{
ret = db.ContactSet.Count();
}
}
catch (Exception)
{
throw;
}
return ret;
}
3.And 最后,您的方法将如下所示:
public JsonResult IndexJson(RequestData model)
{
var searchPhrase = model.Search;
if (!string.IsNullOrWhiteSpace(searchPhrase))
{
//Notice that the select columns match the ContactSetDTO properties
string query = @"SELECT TOP " + model.RowCount + " s.AccountId, s.FirstName, s.LastName, s.FullName, s.JobTitle, s.ParentCustomerId, s.EmailAddress1, s.Telephone1, s.MobilePhone, s.Fax, s.GenderCode, s.BirthDate FROM ContactSet s WHERE s.FirstName LIKE '%' + @p0 + '%' OR s.LastName LIKE '%' + @p0 + '%')";
//Then, this should return a list of ContactSetDTO for you
var result = db.Database.SqlQuery<ContactSetDTO>(query, new System.Data.SqlClient.SqlParameter(parameterName: "@p0", value: searchPhrase)).ToList();
var totalRows = getContactSetCount(searchPhrase);
var tResult = new { rows = result, rowCount = model.RowCount, total = totalRows, current = model.Current, searchPhrase = model.Search }
};
return Json(tResult, JsonRequestBehavior.AllowGet);
}
希望以上内容对您有所帮助。
几天来我一直在努力尝试使用我的 ASP.Net 应用程序实施 jQuery Bootgrid。到目前为止,这就是我所拥有的:(Order By Functionality 还没有工作,我稍后会解决)
public JsonResult IndexJson(RequestData model)
{
var result = (from x in db.ContactSet
select new
{
x.AccountId,
x.FirstName,
x.LastName,
x.FullName,
x.JobTitle,
x.ParentCustomerId,
x.EMailAddress1,
x.Telephone1,
x.MobilePhone,
x.Fax,
x.GenderCode,
x.BirthDate
}); //? Gets all rows
result = (from x in result
where x.FirstName.Contains(model.searchPhrase)
|| x.LastName.Contains(model.searchPhrase)
select x); //? Search Filter
var totalRows = result.Count(); //? Sets totalRows (for ResponseData)
if (model.rowCount == -1)
model.rowCount = totalRows; //? In case View All Rows is selected by Bootgrid (for ResponseData)
// TODO: Add Order By functionality
var tResult = new ResponseData<object>()
{
current = model.current,
rowCount = model.rowCount,
rows = result.ToList(),
total = totalRows
}; //? Builds Json Response
return Json(tResult, JsonRequestBehavior.AllowGet);
}
此代码的问题是我需要在搜索功能后计算记录总数,而我只是不太擅长正确使用 LINQ 查询。
当我到达 var totalRows = result.Count();
时,我收到以下错误:
System.NotSupportedException: 'The method 'Where' cannot follow the method 'Select' or is not supported. Try writing the query in terms of supported methods or call the 'AsEnumerable' or 'ToList' method before calling unsupported methods.'
知道这里出了什么问题吗?
我一直在不同的情况下使用 bootgrid,包括实现服务器端分页和排序 asc,desc 没有任何问题。
试试这个:
//Let's assume this is your model....
public class RequestData
{
public int RowCount { get; set; }
public int Current { get; set; }
public string Search { get; set; }
public string SortBy { get; set; }
public string SortDirection { get; set; }
public int TotalItems { get; set; }
}
1.If 您没有选择数据库的所有列 table,请创建一个 DTO 来映射您选择的列。否则跳过这部分并将 ContactSetDTO 替换为 ContactSet
public class ContactSetDTO
{
public string AccountId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName { get; set; }
public string JobTitle { get; set; }
public string ParentCustomerId { get; set; }
public string EMailAddress1 { get; set; }
public string Telephone1 { get; set; }
public string MobilePhone { get; set; }
public string Fax { get; set; }
public string GenderCode { get; set; }
public DateTime BirthDate { get; set; }
}
2.Assuming您正在使用SQL服务器,您可以使用以下方法获取计数:
public int getContactSetCount(string searchPhrase)
{
int ret = 0;
try
{
string query = string.Empty;
if (!string.IsNullOrWhiteSpace(searchPhrase))
{
// ********* Assuming your db table is also called ContactSet **********************
query = @"SELECT COUNT(*) FROM ContactSet s WHERE s.FirstName LIKE '%' + @p0 + '%' OR s.LastName LIKE '%' + @p0 + '%')";
ret = db.Database.SqlQuery<int>(query, new System.Data.SqlClient.SqlParameter(parameterName: "@p0", value: searchPhrase)).FirstOrDefault();
}
else
{
ret = db.ContactSet.Count();
}
}
catch (Exception)
{
throw;
}
return ret;
}
3.And 最后,您的方法将如下所示:
public JsonResult IndexJson(RequestData model)
{
var searchPhrase = model.Search;
if (!string.IsNullOrWhiteSpace(searchPhrase))
{
//Notice that the select columns match the ContactSetDTO properties
string query = @"SELECT TOP " + model.RowCount + " s.AccountId, s.FirstName, s.LastName, s.FullName, s.JobTitle, s.ParentCustomerId, s.EmailAddress1, s.Telephone1, s.MobilePhone, s.Fax, s.GenderCode, s.BirthDate FROM ContactSet s WHERE s.FirstName LIKE '%' + @p0 + '%' OR s.LastName LIKE '%' + @p0 + '%')";
//Then, this should return a list of ContactSetDTO for you
var result = db.Database.SqlQuery<ContactSetDTO>(query, new System.Data.SqlClient.SqlParameter(parameterName: "@p0", value: searchPhrase)).ToList();
var totalRows = getContactSetCount(searchPhrase);
var tResult = new { rows = result, rowCount = model.RowCount, total = totalRows, current = model.Current, searchPhrase = model.Search }
};
return Json(tResult, JsonRequestBehavior.AllowGet);
}
希望以上内容对您有所帮助。