locked
Filter data using a drop down RRS feed

  • Question

  • User251280784 posted

    Hello peeeps

    I have two tables with a many-to-many relationship. I want to filter the result set from table 1 table with the assigned entities of table 2. I can't seem to get the filter to work properly. Any help would be most appreciated.

    Code below:

    CONTROLLER:

    == CONTROLLER ==

            [HttpPost]

            public ActionResult Index(string filterMarkets)
            {
                var asrs = from a in db.ASR
                           select a;
     
                var markets = from m in db.Market
                              select m;
     
                ViewBag.assignedMarkets = new SelectList(db.Market, "MarketID", "MarketName");
     
                if (!String.IsNullOrEmpty(filterMarkets))
                {
                    asrs = asrs.Where(a => a.FullName.Contains(filterMarkets.ToString()));
                }
                else if (!String.IsNullOrEmpty(filterMarkets))
                {
                    markets = markets.Where(m => m.MarketName.ToUpper().Contains(filterMarkets.ToUpper()));
     
                }
                return View(asrs.ToList());
            }
    ==========

    == VIEW ==

                    @using (Html.BeginForm())
                    {
                        @Html.AntiForgeryToken()
                        @Html.DropDownList("assignedMarkets", ViewBag.MarketsList as SelectList, "-- Filter By Market --", new { @onchange = "this.form.submit();", @class = "btn btn-primary dropdown-toggle btn-sm", @value = "filterMarkets" })
                    }

    ========

    Friday, January 25, 2019 2:01 AM

All replies

  • User475983607 posted

    ViewBag.MarketsList is not assigned a value in the code snippet.  Please review and run your code through the Visual Studio debugger as the code has several logic errors.

    https://docs.microsoft.com/en-us/visualstudio/debugger/navigating-through-code-with-the-debugger?view=vs-2017

    public ActionResult Index(string filterMarkets)
    {
    	var asrs = from a in db.ASR
    	select a;
    	var markets = from m in db.Market
    	select m;
    	ViewBag.assignedMarkets = new SelectList(db.Market, "MarketID", "MarketName");
    	if (!String.IsNullOrEmpty(filterMarkets))
    	{
    		asrs = asrs.Where(a => a.FullName.Contains(filterMarkets.ToString()));
    	}
    	else if (!String.IsNullOrEmpty(filterMarkets))
    	{
    		markets = markets.Where(m => m.MarketName.ToUpper().Contains(filterMarkets.ToUpper()));
    	}
    	return View(asrs.ToList());
    }
    ==========
    == VIEW ==
    @using (Html.BeginForm())
    {
    	@Html.AntiForgeryToken()
    	@Html.DropDownList("assignedMarkets", ViewBag.MarketsList as SelectList, "-- Filter By Market --", new { @onchange = "this.form.submit();", @class = "btn btn-primary dropdown-toggle btn-sm", @value = "filterMarkets" })
    }

    Friday, January 25, 2019 2:46 PM
  • User-943250815 posted

    Is your return correct? It always return asrs

    public ActionResult Index(string filterMarkets) {
      var asrs = from a in db.ASR select a;
      var markets = from m in db.Market select m;
      ViewBag.assignedMarkets = new SelectList(db.Market, "MarketID", "MarketName");
      if (!String.IsNullOrEmpty(filterMarkets)) {
        asrs = asrs.Where(a => a.FullName.Contains(filterMarkets.ToString()));
      }
      else if (!String.IsNullOrEmpty(filterMarkets))
      {
      markets = markets.Where(m => m.MarketName.ToUpper().Contains(filterMarkets.ToUpper()));
      }
      return View(asrs.ToList());
    }

    Friday, January 25, 2019 2:48 PM
  • User251280784 posted

    It should return the asrs. However, the filter should return the filtered asrs results. If you think it should return something other than that, let me know. I am also going to look at the previous response, it mentioned logic errors, I'll see what that's about.

    Friday, January 25, 2019 7:21 PM
  • User1520731567 posted

    Hi CarCancelo,

    I want to filter the result set from table 1 table with the assigned entities of table 2. I can't seem to get the filter to work properly.

    if (!String.IsNullOrEmpty(filterMarkets))
    	{
    		asrs = asrs.Where(a => a.FullName.Contains(filterMarkets.ToString())); //line1
    	}
    	else if (!String.IsNullOrEmpty(filterMarkets))
    	{
    		markets = markets.Where(m => m.MarketName.ToUpper().Contains(filterMarkets.ToUpper())); //line2
    	}
    	return View(asrs.ToList());

    According to your code,this does have a logic error.

    Both line1 and line2 will run,but you only return asrs so that it makes line2 invalid.

    I know you would like to make line1 and line2 contact and filter the data between them.

    And there may be duplicate data.

    So,Since I do not know what is your source,I suggest you could combine use .Intersect(). Except(),.Add(),.Distinct()...

    They will help you handle data.

    Best Regards.

    Yuki Tao

    Monday, January 28, 2019 9:35 AM