2016-08-26 1 views
-1

Ich habe einen Filterteil in meinem Projekt. Ich habe Probleme, die Liste der Artikel zu filtern, die zwischen Min-Preis und Max-Preis fallen. Ich weiß nicht, was ich falsch gemacht habe.Filterliste zwischen minprice und maxprice in mvc4

enter image description here

public ActionResult FilteredResult(string Location, string Type, string Category, string MinValue, string MaxValue, string sortOrder, string currentFilter, string searchString, int? page, string CurrentPageNumber) 
     { 

      ViewBag.CurrentSort = sortOrder; 
      var sdata = currentFilter; 
      if (sdata == null) 
      { 
       ViewBag.CurrentFilter = Location + "/" + Type + "/" + Category + "/" + MinValue + "/" + MaxValue; 
      }else 
      { 
       ViewBag.CurrentFilter = sdata; 
       var data = sdata.Split('/'); 
       Location = data[0] == "" ? "" : data[0]; 
       Type = data[1] == "" ? "" : data[1]; 
       Category = data[2] == "" ? "" : data[2]; 
       MinValue = data[3] == "" ? "" : data[3]; 
       MaxValue = data[4] == "" ? "" : data[4]; 
      } 

      if (searchString != null) 
      { 
       page = 1; 
      } 
      else 
      { 
       searchString = currentFilter; 
      } 
      List<ListingItem> FilteredListing = new List<ListingItem>(); 
      var Property = from s in db.RE_PropertyDetails 
          where (s.IsActive == true) 
          select s; 
      var PropertPaginate = from s in db.RE_PropertyDetails 
            where (s.IsActive == true) 
            select s; 

      if (!String.IsNullOrEmpty(Location) && String.IsNullOrEmpty(Type) && String.IsNullOrEmpty(Category)) 
      { 

       Property = Property.Where(s => s.Location.ToUpper().Contains(Location.ToUpper())); 
       PropertPaginate = PropertPaginate.Where(s => s.Location.ToUpper().Contains(Location.ToUpper())); 
      } 
      else if (String.IsNullOrEmpty(Location) && !String.IsNullOrEmpty(Type) && String.IsNullOrEmpty(Category)) 
      { 

       var typ = Convert.ToInt32(Type); 
       Property = Property.Where(s => s.TypeId == typ); 
       PropertPaginate = PropertPaginate.Where(s => s.TypeId == typ); 

      } 
      else if (String.IsNullOrEmpty(Location) && String.IsNullOrEmpty(Type) && !String.IsNullOrEmpty(Category)) 
      { 
       var cat = Convert.ToInt32(Category); 

       Property = Property.Where(s => s.PropertyCategoryId == cat); 
       PropertPaginate = PropertPaginate.Where(s => s.PropertyCategoryId == cat); 

      } 
      else if (!String.IsNullOrEmpty(Location) && String.IsNullOrEmpty(Type) && !String.IsNullOrEmpty(Category)) 
      { 
       var cat = Convert.ToInt32(Category); 

       Property = Property.Where(s => s.Location.ToUpper().Contains(Location.ToUpper()) 
            && s.PropertyCategoryId == cat); 
       PropertPaginate = PropertPaginate.Where(s => s.Location.ToUpper().Contains(Location.ToUpper()) 
            && s.PropertyCategoryId == cat); 

      } 
      else if (!String.IsNullOrEmpty(Location) && !String.IsNullOrEmpty(Type) && String.IsNullOrEmpty(Category)) 
      { 

       var typ = Convert.ToInt32(Type); 
       Property = Property.Where(s => s.Location.ToUpper().Contains(Location.ToUpper()) && s.TypeId == typ); 
       PropertPaginate = PropertPaginate.Where(s => s.Location.ToUpper().Contains(Location.ToUpper()) && s.TypeId == typ); 
      } 
      else if (String.IsNullOrEmpty(Location) && !String.IsNullOrEmpty(Type) && !String.IsNullOrEmpty(Category)) 
      { 
       var cat = Convert.ToInt32(Category); 
       var typ = Convert.ToInt32(Type); 
       Property = Property.Where(s => s.TypeId == typ 
            && s.PropertyCategoryId == cat); 
       PropertPaginate = PropertPaginate.Where(s => s.TypeId == typ 
           && s.PropertyCategoryId == cat); 

      } 
      else if (!String.IsNullOrEmpty(Location) && !String.IsNullOrEmpty(Type) && !String.IsNullOrEmpty(Category)) 
      { 
       var cat = Convert.ToInt32(Category); 
       var typ = Convert.ToInt32(Type); 
       Property = Property.Where(s => s.Location.ToUpper().Contains(Location.ToUpper()) 
            && s.PropertyCategoryId == cat 
            && s.TypeId == typ); 

       PropertPaginate = PropertPaginate.Where(s => s.Location.ToUpper().Contains(Location.ToUpper()) 
           && s.PropertyCategoryId == cat 
            && s.TypeId == typ); 

      } 
      else 
      { 

      } 

      var maxval = Convert.ToDecimal(MaxValue); 
      var minval = Convert.ToDecimal(MinValue); 
      var otherproperty = Property.Where(s => s.Price >= minval && s.Price <= maxval).OrderBy(s => s.Price); 
      Property = otherproperty; 
      var otherPropertPaginate = Property.Where(s => s.Price >= minval && s.Price <= maxval).OrderBy(s => s.Price); 
      PropertPaginate = otherPropertPaginate; 



      foreach (var item in Property) 
      { 
       ListingItem obj = new ListingItem(); 

       obj.PropertyID = item.PropertyID; 
       obj.PropertName = item.PropertName; 
       obj.ImageId = db.RE_ImageDetail.Where(p => p.PropertyId == item.PropertyID).Select(p => p.ImageId).FirstOrDefault(); 
       obj.ImageName = db.RE_ImageDetail.Where(p => p.PropertyId == item.PropertyID).Select(p => p.ImageName).FirstOrDefault(); 
       obj.ParkingArea = item.ParkingArea; 
       obj.BedRoom = item.BedRoom; 
       obj.BathRoom = item.BathRoom; 
       obj.LivingRoom = item.LivingRoom; 
       obj.Price = Convert.ToDecimal(item.Price); 
       obj.Area = item.Area.ToString(); 
       FilteredListing.Add(obj); 
      } 
      ViewBag.FilterListing = FilteredListing; 
      ViewBag.CountedNumber = Property.Count(); 

      var Maxvalue = db.RE_PropertyDetails.Select(o => o.Price).Max(); 
      ViewBag.MaxPrice = Maxvalue; 
      var Minvalue = db.RE_PropertyDetails.Select(o => o.Price).Min(); 
      ViewBag.Minvalue = Minvalue; 



      switch (sortOrder) 
      { 
       case "Date: Ascending": 
        PropertPaginate = PropertPaginate.OrderBy(s => s.AddedonDate); 
        break; 
       case "Date: Descending": 
        PropertPaginate = PropertPaginate.OrderByDescending(s => s.AddedonDate); 
        break; 
       case "Price: Lowest First": 
        PropertPaginate = PropertPaginate.OrderBy(s => s.Price.HasValue); 
        break; 
       case "Price: Highest First": 
        PropertPaginate = PropertPaginate.OrderByDescending(s => s.Price.HasValue); 
        break; 
       default: // Name ascending 
        PropertPaginate = PropertPaginate.OrderBy(s => s.PropertName); 
        break; 
      } 

      int pageSize = Convert.ToInt32(CurrentPageNumber == null ? "10" : CurrentPageNumber); 
      int pageNumber = (page ?? 1); 
      ViewBag.page = page == null ? 1 : pageNumber; 
      return View(PropertPaginate.ToPagedList(pageNumber, pageSize)); 

     } 

Ich bin der Min- und Max-Wert als String bekommen, dann schrieb ich den Code

var maxval = Convert.ToDecimal(MaxValue); 
      var minval = Convert.ToDecimal(MinValue); 
      var otherproperty = Property.Where(s => s.Price >= minval && s.Price <= maxval).OrderBy(s => s.Price); 
      Property = otherproperty; 

Aber es Daten ergibt, die nicht innerhalb der bounderies von min und max Preis. manchmal liefert es keine Daten oder manchmal, wenn der Maximalwert ein Bit erhöht, zeigt es 4-5 Daten.

zum Beispiel gibt es Daten mit Preis 11,200000,500000, ..... wenn minvalue = 10 und maxvalue = 20191023 Wie Sie sehen können, ist es keine Daten, die zeigen

wenn minvalue 10 = und maxvalue = 400000 es zeigt Daten bis 500000

Kann mir jemand vorschlagen, was ich falsch mache. Danke

+1

Warum in der Welt machen Sie Ihre Parameter 'string', wenn die meisten klar' int? 'Oder' decimal? 'Etc (und dann müssen sie konvertieren). Und ändere all deinen Code, um einfach 'if (! String.IsNullOrEmpty (Ort) {...}; if (typ.HasValue) {...}; if (cat.HasValue) {...}; –

+0

ich habe die Änderungen vorgenommen, wie Sie aber über das Element zwischen der Preisklasse vorgeschlagen haben, ist nicht gezeigt können Sie meinen Code '' var maxwert = Convert.ToDecimal (MaxValue) überprüfen; var minval = Convert.ToDecimal (MinValue); var otherproperty = Property.Where (s => s.Preis> = minval && s.Preis <= maxval) .OrderBy (s => sPrice); Eigenschaft = otherproperty; var otherPropertPaginate = Eigenschaft .Wobei (s => s.Preis> = minval && s.Preis <= maxval) .OrderBy (s => sPrice); PropertyPaginate = otherPropertPaginate; '' – Avinash

+0

Sorry, um hart zu sein, aber nichts in deinem Code macht irgendeinen Sinn. Wenn Sie Werte zurückschreiben, die an Ihre Parameter gebunden sind, dann ignorieren Sie sie und überschreiben sie mit Werten aus Ihrem 'currentFilter'-Parameter, die eindeutig nicht die gleichen Werte haben, die Ihr Beitrag und damit Ihre Ergebnisse nicht korrekt sind. Und warum in der Welt schaffen Sie 2 identische Sammlungen ('Property' und' PropertyPaginate') –

Antwort

0

Warum Sie den string verwenden, es ist eindeutig ein int oder decimal Werte haben muss.