Ejemplo n.º 1
0
        public  ActionResult FindMember(FindMemberViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = from u in this.AppDbContext.Users
                             join m in this.AppDbContext.Members
                             on u.UserName equals m.MemberID
                             orderby m.RegisterDate descending
                             select new MemberInfoModel
                             {
                                 MemberID = m.MemberID,
                                 ReferenceID = m.ReferenceMemberID,
                                 Name = m.Name,
                                 IDCard = m.IDCard,
                                 Address = m.Address,
                                 Phone = u.PhoneNumber,
                                 Level = m.Level
                             };

                if (string.IsNullOrWhiteSpace(model.MemberID) == false)
                {
                    //OR --> use False
                    var predicate = PredicateBuilder.False<MemberInfoModel>();

                    predicate = predicate.Or(m => m.MemberID != null
                                  && m.MemberID.StartsWith(model.MemberID));

                    predicate = predicate.Or(m => m.ReferenceID != null && m.ReferenceID.StartsWith(model.MemberID,StringComparison.InvariantCultureIgnoreCase));

                    predicate = predicate.Or(m => m.Name != null && m.Name.IndexOf(model.MemberID,StringComparison.InvariantCultureIgnoreCase) > -1);

                    predicate = predicate.Or(m => m.IDCard != null && m.IDCard.StartsWith(model.MemberID, StringComparison.InvariantCultureIgnoreCase));

                    predicate = predicate.Or(m => m.Phone != null && m.Phone.StartsWith(model.MemberID, StringComparison.InvariantCultureIgnoreCase));

                    result = result.Where(predicate.Compile()).AsQueryable();
                }

                

                int size = result.Count();

                // paging
                result = result.Skip(model.page * model.PageSize).Take(model.PageSize);

                var items = result.ToList();

                return Json(new
                {
                    TotalSize = size,
                    Members = items
                }, JsonRequestBehavior.AllowGet);

            }
            return JsonMessage.BadRequestJsonResult(ModelState.Values.SelectMany(x => x.Errors));

        }
Ejemplo n.º 2
0
        public async Task<IActionResult> FindMember(FindMemberViewModel model) {
            if (ModelState.IsValid)
            {
                var result = from u in _applicationDbContext.Users
                             join m in _applicationDbContext.Members
                             on u.UserName equals m.MemberID
                             orderby m.RegisterDate descending
                             select new MemberInfoModel
                             {
                                 MemberID = m.MemberID,
                                 ReferenceID = m.ReferenceMemberID,
                                 Name = m.Name,
                                 IDCard = m.IDCard,
                                 Address = m.Address,
                                 Phone = u.PhoneNumber,
                                 Level = m.Level
                             };

                if (string.IsNullOrWhiteSpace(model.MemberID) == false) {
                    result = result.Where(
                        m =>  m.MemberID != null 
                              && m.MemberID.StartsWith(model.MemberID, StringComparison.InvariantCultureIgnoreCase));
                }
                
                if (string.IsNullOrWhiteSpace(model.ReferenceID) == false)
                {
                    result = result.Where(m => m.ReferenceID != null && m.ReferenceID.StartsWith(model.ReferenceID, 
                                                                        StringComparison.InvariantCultureIgnoreCase));
                }
                if (string.IsNullOrWhiteSpace(model.Name) == false)
                {
                    result = result.Where(m => m.Name != null && m.Name.IndexOf(model.Name) > -1);
                }

                if (string.IsNullOrWhiteSpace(model.IDCard) == false)
                {
                    result = result.Where(
                        m => m.IDCard != null && m.IDCard.StartsWith(model.IDCard, 
                                                     StringComparison.InvariantCultureIgnoreCase));
                }

                if (string.IsNullOrWhiteSpace(model.Phone) == false)
                {
                    result = result.Where(
                        m => m.Phone != null && m.Phone.StartsWith(model.Phone, 
                                                                    StringComparison.InvariantCultureIgnoreCase));
                }
       
               int size = await result.CountAsync();

              // paging
               result = result.Skip(model.page * model.PageSize).Take(model.PageSize);

              var items = result.ToList();

                return Json(new
                {
                    TotalSize = size,
                    Members = items
                });
           
            }
            return ErrorMessage.BadRequestJsonResult(ModelState.Values.SelectMany(x => x.Errors));

        }