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]
<div> public ActionResult Index(string filterMarkets)</div> <div> {</div> <div> var asrs = from a in db.ASR</div> <div> select a;</div> <div></div>
<div> var markets = from m in db.Market</div> <div> select m;</div> <div></div> <div> ViewBag.assignedMarkets = new SelectList(db.Market, "MarketID", "MarketName");</div>
<div></div> <div> if (!String.IsNullOrEmpty(filterMarkets))</div> <div> {</div> <div> asrs = asrs.Where(a => a.FullName.Contains(filterMarkets.ToString()));</div> <div>
}</div> <div> else if (!String.IsNullOrEmpty(filterMarkets))</div> <div> {</div> <div> markets = markets.Where(m => m.MarketName.ToUpper().Contains(filterMarkets.ToUpper()));</div>
<div></div> <div> }</div> <div> return View(asrs.ToList());</div> <div> }</div> <div>==========</div>
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.
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());
}
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.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
26 Posts
Filter data using a drop down
Jan 25, 2019 02:01 AM|CarCancelo|LINK
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]
<div> public ActionResult Index(string filterMarkets)</div> <div> {</div> <div> var asrs = from a in db.ASR</div> <div> select a;</div> <div> </div> <div> var markets = from m in db.Market</div> <div> select m;</div> <div> </div> <div> ViewBag.assignedMarkets = new SelectList(db.Market, "MarketID", "MarketName");</div> <div> </div> <div> if (!String.IsNullOrEmpty(filterMarkets))</div> <div> {</div> <div> asrs = asrs.Where(a => a.FullName.Contains(filterMarkets.ToString()));</div> <div> }</div> <div> else if (!String.IsNullOrEmpty(filterMarkets))</div> <div> {</div> <div> markets = markets.Where(m => m.MarketName.ToUpper().Contains(filterMarkets.ToUpper()));</div> <div> </div> <div> }</div> <div> return View(asrs.ToList());</div> <div> }</div> <div>==========</div>== VIEW ==
<div> @using (Html.BeginForm())</div> <div> {</div> <div> @Html.AntiForgeryToken()</div> <div> @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" })</div> <div> }</div>========
All-Star
52201 Points
23274 Posts
Re: Filter data using a drop down
Jan 25, 2019 02:46 PM|mgebhard|LINK
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" }) }
Participant
1051 Points
658 Posts
Re: Filter data using a drop down
Jan 25, 2019 02:48 PM|jzero|LINK
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());
}
None
0 Points
26 Posts
Re: Filter data using a drop down
Jan 25, 2019 07:21 PM|CarCancelo|LINK
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.
Contributor
3710 Points
1431 Posts
Re: Filter data using a drop down
Jan 28, 2019 09:35 AM|Yuki Tao|LINK
Hi CarCancelo,
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
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.