public DatatableData <News> GetNewsRecords(Nullable <int> start, Nullable <int> length, string sortDir, string sortCol) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "GetNewsRecords"; cmd.Parameters.Clear(); cmd.Parameters.Add("@start", SqlDbType.Int).Value = start; cmd.Parameters.Add("@length", SqlDbType.Int).Value = length; cmd.Parameters.Add("@sortDirVal", SqlDbType.NVarChar).Value = sortDir; cmd.Parameters.Add("@sortCol", SqlDbType.NVarChar).Value = sortCol; cmd.Parameters.Add("@recordsTotal", SqlDbType.Int).Direction = ParameterDirection.Output; cmd.Parameters.Add("@recordsFiltered", SqlDbType.Int).Direction = ParameterDirection.Output; string res = string.Empty; var dtObj = new DatatableData <News>(); try { if (cmd.Connection.State != ConnectionState.Open) { cmd.Connection.Open(); } var reader = cmd.ExecuteReader(); var TBLList = Utilities.GetProcedureToList <News>(reader); reader.Close(); conn.Close(); dtObj.recordsTotal = Convert.ToInt32(cmd.Parameters["@recordsTotal"].Value); dtObj.recordsFiltered = Convert.ToInt32(cmd.Parameters["@recordsFiltered"].Value); dtObj.data = TBLList; } catch (Exception ex) { throw ex; } return(dtObj); }
//datatableRequest Property Value //----------------------------------- //Data=null //Index=1 //Page=0 //Size=9 //StartIndex=0 //Total=0 public ActionResult GetItems(DatatableData <ProductViewModel> datatableRequest, int?categoryId) { var imgPath = Constants.ImagePath + "/"; //entities is of type IQueryable<Product> var entities = _productService.GetAll().Where(a => a.Status == (int)Constants.EnumProductStatus.Stocking); if (categoryId.HasValue && categoryId > 0) { entities = entities.Where(a => a.CategoryId == categoryId.Value || a.Category.ParentId == categoryId); } //models is of type IQueryable<ProductViewModel> //Map Product.Id to ProductViewModel.Id, begitu juga pada Price, ProductName, Image //Image -> get product images if any -> get main image -> then append it to image path , otherwise it's null //Order by price ascending //Convert Product which is mapped above to ProductViewModel var models = entities.Select(a => new ProductViewModel() { Id = a.Id, Price = a.Price, ProductName = a.Name, Image = a.Images.Any() ? a.Images.Any(x => x.Main == true) ? imgPath + a.Images.FirstOrDefault(x => x.Main == true).Name : imgPath + a.Images.FirstOrDefault().Name : null }).OrderBy(a => a.Price); return(Json(DatatableModel.Refresh(models, datatableRequest.Size, datatableRequest.Index))); }
public JsonResult GetNewsRecords() { var lst = new DatatableData <News>(); var vdata = new DatatableData <News>(); try { dynamic obj = null; int start = 0; int length = 0; string searchVal = ""; string sortDirVal = ""; int sortVal = 0; string sortCol = ""; int tableLength = 0; MemoryStream stream = new MemoryStream(); Request.Body.CopyTo(stream); stream.Position = 0; using (StreamReader reader = new StreamReader(stream)) { string requestBody = reader.ReadToEnd(); if (requestBody.Length > 0) { obj = JsonConvert.DeserializeObject(requestBody); if (obj != null) { searchVal = obj.parameters.search.value; start = obj.parameters.start; length = obj.parameters.length; sortDirVal = obj.parameters.order.First.dir; sortVal = obj.parameters.order.First.column; sortCol = obj.parameters.columns[sortVal].name; if (sortCol == "") { sortCol = "CreationDate"; } tableLength = obj.parameters.columns.Count; } } } TimesOfLebanonContext dbContext = new TimesOfLebanonContext(); var Helper = new Helper(); lst = Helper.GetNewsRecords(start, length, sortDirVal, sortCol); vdata = lst; vdata.draw = obj.parameters.draw; } catch (Exception ex) { var _User = HttpContext.Session.GetObjectFromJson <Users>("User"); HandleException(_User.UserId, "GetNewsRecords", ex); } return(new JsonResult(vdata)); }
public ActionResult GetItems(DatatableData <ProductViewModel> datatableRequest, int?categoryId) { var imgPath = Constants.ImagePath + "/"; var entities = _productService.GetAll().Where(a => a.Status == (int)Constants.EnumProductStatus.Stocking); if (categoryId.HasValue && categoryId > 0) { entities = entities.Where(a => a.CategoryId == categoryId.Value || a.Category.ParentId == categoryId); } var models = entities.Select(a => new ProductViewModel() { Id = a.Id, Price = a.Price, ProductName = a.Name, Image = a.Images.Any() ? a.Images.Any(x => x.Main == true) ? imgPath + a.Images.FirstOrDefault(x => x.Main == true).Name : imgPath + a.Images.FirstOrDefault().Name : null }).OrderBy(a => a.Price); return(Json(DatatableModel.Refresh(models, datatableRequest.Size, datatableRequest.Index))); }
public News GetNewsById(Int64 Id) { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "GetNewsById"; cmd.Parameters.Clear(); cmd.Parameters.Add("@Id", SqlDbType.BigInt).Value = Id; string res = string.Empty; var dtObj = new DatatableData <News>(); try { if (cmd.Connection.State != ConnectionState.Open) { cmd.Connection.Open(); } var reader = cmd.ExecuteReader(); var TBLList = Utilities.GetProcedureToList <News>(reader); reader.Close(); conn.Close(); if (TBLList.Count > 0) { return(TBLList[0]); } else { return(null); } } catch (Exception ex) { throw ex; } }