2016-08-26 5 views
-1

私のプロジェクトにはフィルターの部分があります。最小価格と最大価格の間にある商品のリストをフィルタリングする問題があります。私は何が間違っていたのか分かりません。mvc4のminpriceとmaxpriceの間のフィルターリスト

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)); 

     } 

私は、私は、コード

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; 

を書きました。しかし、それは最小と最大のbounderies内にないデータを得ている、文字列として最小値と最大値を取得しています価格。場合によってはデータを生成しないか、または最大値がビット増加である場合には、それは4-5データを示す。

例 の価格11,200000,500000とのデータがある、..... あなたはMINVALUE = 10時にもデータ

を示していない見ることができるようにMINVALUEは10とMAXVALUE = 20191023 を=とmaxvalue = 400000 それは500000までのデータを表示します

私が間違っていることを誰にでも教えてもらえますか? あなたが

+1

大部分が明らかに 'int?'か 'decimal?'などで(そして変換しなければならない)あなたのパラメータを 'string'にしているのはなぜですか?そして、if(cat.HasValue){...}; 'などのようにif(!String.IsNullOrEmpty(Location){...}; if(typ.HasValue){...} –

+0

私はあなたが提案してきたように変更を加えたが、価格帯の間でのアイテムについて示されていないあなたは私のコード '' する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); プロパティ= otherproperty; var otherPropertPaginate =プロパティ.OhereBy(s => s.Price); PropertPaginate = otherPropertPaginate; '' – Avinash

+0

大変申し訳ありませんが、あなたのコードには何の意味もありません。あなたのパラメータにバインドされたあなたのポストバック値は、それらを無視して、あなたの投稿と同じ値ではない明らかにあなたの 'currentFilter'パラメータからの値で上書きするので、結果は正しくありません。なぜ世界で2つの同一のコレクション( 'Property'と' PropertPaginate')を作成しているのですか? –

答えて

0

あなたがstringを使用している理由、明らかにintまたはdecimal値を持っている必要がありますありがとうございました。

public ActionResult FilteredResult(string Location, string Type, string Category, decimal? MinValue, decimal? MaxValue, int sortOrder, string currentFilter, string searchString, int? page, string CurrentPageNumber) 
{ 
    // Your stuff.. 
} 
関連する問題