I'm facing problems with server side pagination in jqgrid using mvc. Here's my controller code.
[HttpGet]
public JsonResult GetAllStudents(JqGridRequest jRequest) {
var Records = from a in entities.studentdetails.ToList() select new {
a.ID,
a.Name,
a.DOB
};
int pageIndex = Convert.ToInt32(jRequest.PageIndex);
int pageSize = jRequest.RecordsCount;
int startRow = (pageIndex * pageSize) + 1;
int totalRecords = Records.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
Records = Records.Skip(pageIndex * pageSize).Take(pageSize).ToArray();
if (jRequest.Searching) {
int sId = Convert.ToInt32(jRequest.SearchingFilters.Filters[0].SearchingValue);
//string sName = jRequest.SearchingFilters.Filters[0].SearchingValue;
Records = entities.studentdetails.Select(a => new {
a.ID,
a.Name,
a.DOB
}).Where(p => p.ID == sId);
}
else {
Records = entities.studentdetails.Select(a => new {
a.ID,
a.Name,
a.DOB
});
}
var jsonData = new {
total = totalPages,
page = pageIndex,
records = totalRecords,
rows = Records
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
Here are some changes I made and the issue got solved.
1- int pageIndex = Convert.ToInt32(jRequest.PageIndex) + 1;
2- Records =
entities.studentdetails.Select(
a => new
{
a.ID,
a.Name,
a.DOB
})**.OrderBy(s => s.ID).Skip((pageIndex-1) * pageSize).Take(pageSize).ToArray();**
Related
I want this query in lambda expression:
Insert into Tbl_Bill (EmpID, TotalBill, BillByEmployee, BillBySBI, CreatedDate, UpdatedDate, OrderIDs)
select
EmpID, Sum(TotalBill), Sum(TotalBill), Sum(TotalBill),
MIN(CreatedDate), MIN(CreatedDate), COUNT(OrderID)
from
Tbl_Order
group by
EmpID;
Use LinqPad, its a superb tool to queries to linq queries
var queryTbl_Bill =
from Tbl_Order in db.Tbl_Order
group Tbl_Order by new
{
Tbl_Order.EmpID
} into g
select new
{
g.Key.EmpID,
Column1 = g.Sum(p => p.TotalBill),
Column2 = g.Sum(p => p.TotalBill),
Column3 = g.Sum(p => p.TotalBill),
Column4 = g.Min(p => p.CreatedDate),
Column5 = g.Min(p => p.CreatedDate),
Column6 = g.Count(p => p.OrderID != null)
};
foreach (var q in queryTbl_Bill)
{
decimal sbi = 0, emp = 0;
if (q.Column1 < 100.0M)
{
sbi = q.Column1 * 0.75M;
emp = q.Column1 * 0.25M;
}
else if (q.Column1 > 100.0M)
{
sbi = 75.0M;
decimal temp = q.Column1 - 100.0M;
emp = temp + 25.0M;
}
Tbl_Bill iTbl_Bill = new Tbl_Bill
{
EmpID = q.EmpID,
TotalBill = q.Column1,
BillByEmployee = emp,
BillBySBI = sbi,
CreatedDate = DateTime.Now,
UpdatedDate = DateTime.Now,
OrderIDs = q.Column6.ToString(),
Status=true
};
db.Tbl_Bill.Add(iTbl_Bill);
}
db.SaveChanges();
i got answer using linqer
I am working on user roles using Asp.net MVC. I am stuck while working on Admin section. I have mentioned one question above and the second question is similar which is Using the generic type 'System.Collections.Generic.List' requires 1 type arguments
Here is my code.
public ActionResult Index(string searchStringUserNameOrEmail, string currentFilter, int? page)
{
try
{
int intPage = 1;
int intPageSize = 5;
int intTotalPageCount = 0;
if (searchStringUserNameOrEmail != null)
{
intPage = 1;
}
else
{
if (currentFilter != null)
{
searchStringUserNameOrEmail = currentFilter;
intPage = page ?? 1;
}
else
{
searchStringUserNameOrEmail = "";
intPage = page ?? 1;
}
}
ViewBag.CurrentFilter = searchStringUserNameOrEmail;
List col_UserDTO = new List();
int intSkip = (intPage - 1) * intPageSize;
intTotalPageCount = UserManager.Users
.Where(x => x.UserName.Contains(searchStringUserNameOrEmail))
.Count();
var result = UserManager.Users
.Where(x => x.UserName.Contains(searchStringUserNameOrEmail))
.OrderBy(x => x.UserName)
.Skip(intSkip)
.Take(intPageSize)
.ToList();
foreach (var item in result)
{
ExpandedUserDTO objUserDTO = new ExpandedUserDTO();
objUserDTO.UserName = item.UserName;
objUserDTO.Email = item.Email;
objUserDTO.LockoutEndDateUtc = item.LockoutEndDateUtc;
col_UserDTO.Add(objUserDTO);
}
// Set the number of pages
// Error appears here
var _UserDTOAsIPagedList =
new StaticPagedList
(
col_UserDTO, intPage, intPageSize, intTotalPageCount
);
return View(_UserDTOAsIPagedList);
}
catch (Exception ex)
{
ModelState.AddModelError(string.Empty, "Error: " + ex);
List col_UserDTO = new List(); // Error appears here
return View(col_UserDTO.ToPagedList(1, 25));
}
}
#endregion
`
StaticPagedList is generic. You need to supply the type of collection(for col_UserDTO), in your case List:
var _UserDTOAsIPagedList =
new StaticPagedList<List<ExpandedUserDTO>>
(
col_UserDTO, intPage, intPageSize, intTotalPageCount
);
See http://www.programering.com/a/MTN2gDNwATM.html
You may need to change List col_UserDTO references to List<ExpandedUserDTO> col_UserDTO
Use this instead
var _UserDTOAsIPagedList =
new StaticPagedList<ExpandedUserDTO>
(
col_UserDTO, intPage, intPageSize, intTotalPageCount
);
I am new to asp .net mvc and I am trying to implement IPagedList paging feature with viewmodel and join. Paging and searching is working fine for normal pages but I am not able to do with viewmodel and joins.
I am following this approach: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
Here is my ViewModel Class:
public class SponserDisplayViewModel
{
public Sponser Sponser { get; set; }
public SponserDetail SponserDetail { get; set; }
public SponserType SponserType { get; set; }
} //--- Here All three are different classes.
Here What I have tried in Controller:
public ActionResult Index(string searchString, int? page, string btnSearch)
{
var viewModel = from s in db.Sponsers
join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
from st in st2.DefaultIfEmpty()
select new SponserDisplayViewModel { Sponser = s, SponserType = st };
if (btnSearch == "Reset")
{ searchString = string.Empty; }
if (!String.IsNullOrEmpty(searchString))
{
viewModel = from s in db.Sponsers
join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
from st in st2.DefaultIfEmpty()
where st.Name.Contains(searchString)
select new SponserDisplayViewModel { Sponser = s, SponserType = st };
}
int pageSize = 20;
int pageIndex = 1;
pageIndex = page.HasValue ? Convert.ToInt32(page) : 1;
//==================Getting error here
IPagedList<SponserDisplayViewModel> po = from s in db.Sponsers
join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
from st in st2.DefaultIfEmpty().OrderBy(a => a.DisplayOrder).ToPagedList(pageIndex, pageSize)
select new SponserDisplayViewModel { Sponser = s, SponserType = st };
return View(po);
}
Please suggest
Got my Answer:
var po = from s in db.Sponsers
join st in db.SponserTypes on s.SponserTypeId equals st.Id into st2
from st in st2.DefaultIfEmpty
orderby st.DisplayOrder
select new SponserDisplayViewModel { Sponser = s, SponserType = st };
return View(po.ToPagedList(pageIndex, pageSize));
its working for me
int pagesize = 7, pageindex = 1;
pageindex = page.HasValue ? Convert.ToInt32(page) : 1;
var list = db.tbl_Category.Where(x => x.C_Status == 1).ToList();
IPagedList<tbl_Category> showlis = list.ToPagedList(pageindex, pagesize);
I have applied search method in my project but , I have also Action index()
and getting erorr. (The current request for action 'Index' on controller type 'AdultLiteracyTeachersController' is ambiguous between the following action methods:)
public ViewResult Index(string searchstring, string currentFilter, int? page)
{
if (searchstring != null)
{
page = 1;
}
else
{
searchstring = currentFilter;
}
ViewBag.CurrentFilter = searchstring;
var teachers = from r in db.AdulLiteracyTeachers select r;
if (!string.IsNullOrEmpty(searchstring))
{
teachers = teachers.Where(r => r.ALTName.ToUpper().Contains(searchstring.ToUpper()));
}
teachers = teachers.OrderBy(r => r.ALTName);
int pagesize = 10;
int pageNo = (page ?? 1);
return View(teachers.ToPagedList(pageNo, pagesize));
}
The other ActionResult.index()
public ActionResult Index()
{
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToList());
}
Action result is used for calling all data how can I aplly in single index ?
Combine them:
public ViewResult Index(string searchstring, string currentFilter, int? page)
{
if (searchString == null && currentFilter == null && !page.HasValue)
{
// No parameters
var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
return View(adulliteracyteachers.ToList());
}
// Regular code here down
if (searchstring != null)
{
page = 1;
}
else
{
searchstring = currentFilter;
}
ViewBag.CurrentFilter = searchstring;
var teachers = from r in db.AdulLiteracyTeachers select r;
if (!string.IsNullOrEmpty(searchstring))
{
teachers = teachers.Where(r => r.ALTName.ToUpper().Contains(searchstring.ToUpper()));
}
teachers = teachers.OrderBy(r => r.ALTName);
int pagesize = 10;
int pageNo = (page ?? 1);
return View(teachers.ToPagedList(pageNo, pagesize));
}
I'd like to order some products based on prices. What are the parameters I pass to my action and how do I use it in sorting from an IQueryable<Product>?
For eg. this is what I currently have:
public ActionResult Index(int page =0, int cat = 0, int subCat =0)
{
var products = productService.GetAllProducts();
ProductListViewModel model = new ProductListViewModel();
if (cat > 0)
{
products = products.Where(p => p.SubCategory.CategoryId == cat);
}
if (subCat > 0)
{
products = products.Where(p => p.SubCategory.SubCategoryId == subCat);
}
products = products.OrderBy(p => p.CreatedDate);
model.PagedProducts = products.ToPagedList(page, 15);
return View(model);
}
You may checkout the following article.
You may pass any variable you want. An int should be enough but you may also use a string.
public ActionResult Index(int page =0, int cat = 0, int subCat =0, int order = 0)
{
var products = productService.GetAllProducts();
ProductListViewModel model = new ProductListViewModel();
if (cat > 0)
{
products = products.Where(p => p.SubCategory.CategoryId == cat);
}
if (subCat > 0)
{
products = products.Where(p => p.SubCategory.SubCategoryId == subCat);
}
switch(order)
{
case 0:
products = products.OrderBy(p => p.CreatedDate);
break;
case 1:
products = products.OrderBy(p => p.Price);
break;
}
model.PagedProducts = products.ToPagedList(page, 15);
return View(model);
}
Replace
products = products.OrderBy(p => p.CreatedDate);
with
products = products.OrderBy(p => p.Price);
If you have a bool flag that you pass in then you can use it to toggle the sort:
public ActionResult Index(int page, int cat, int subCat, int order, bool isAscending)
{
var products = productService.GetAllProducts();
if (isAscending)
products = products.OrderBy(prod => prod.Price);
else
products = products.OrderByDescending(prod => prod.Price);
// Other code...
}