1

I am pretty certain my lack of understanding is preventing me form getting it to work.

I currently have a form that will post filtering parameters back to the controller, then it will build up the query. The current queries I build does not seam to actually do anything. At least not when it is separated like this.

From what I have read, something like this should work, but from what I have tested, it does not.

[HttpPost]
public async Task<IActionResult> FilterPost()
{
    var query = _context.Products;
    query = query.OrderBy(m => m.Unit.Name)
         .Include(m => m.Unit)
         .Include(m => m.Attribute);

    foreach (var request in Request.Form)
    {
         var AttributeValue = request.Value;
         var AttributeName = request.Key;
         query = query.Where(m => m.Attribute.Name == AttributeName)
                      .Where(m => m.Value >= int.Parse(AttributeValue));
    }

    return RedirectToAction("Index");
}

This method is probably pretty butchered with all the testing I have done on it.

I have looked into using ExpressionTrees and DynamicLINQ before. They do seam confusingly interestring and perhaps a bit overkill, maybe? I dont know. Maybe I my brain is just too exhausted to see the obvious answer...

I would appreciate any kind of suggestion for a solution or a hint towards an solution.

jarlh
  • 42,561
  • 8
  • 45
  • 63
Intekin
  • 15
  • 1
  • 6
  • 1
    You are not passing any model back to your view. Whatever result is produced by your query remains in the controller. See the overloads of RedirectToAction or this answer https://stackoverflow.com/questions/11209191/how-do-i-include-a-model-with-a-redirecttoaction – Steve Jan 31 '20 at 09:11
  • You should return the result to be able to get a response. – Florim Maxhuni Jan 31 '20 at 09:12
  • 1
    "This method is probably pretty butchered with all the testing I have done on it." - then FIX IT. Btw., nice to put together aquery - it is NEVER EXECUTED. You redirect to /Index without any filtering. – TomTom Jan 31 '20 at 09:25
  • Yeah, it does not post anything back to the index because I am just using breakpoints to see what is happening with the query. One step at a time. But I will take all of your suggestions and tips, and try to bash it in to my thick skull. Thank you. – Intekin Jan 31 '20 at 10:00

1 Answers1

0

Also, set breakpoint and inspect what is inside request. Moreover, you filter data based on request fields, but in each loop iteration you are filtering result more and more, so you have to consider this carefully.

Also, when redirecting to Index, you don't pass any data and I think you should pass result of a query there, so you can use it and show it there. So I'd take look at overloads of RedirectToAction such as RedirectToAction(String, Object). Alternatively, you can take a look at View method with similair capabilities as RedirectToAction.

Having said that, you shuld pass RESULTS of a query, so you need to get the result of a query, for example, by calling ToList method.

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • It is a post method because he posts the form back to the controller. – TomTom Jan 31 '20 at 09:25
  • @TomTom Agree, but it could be done via query string parameters. Post method (as far as I know) should be used for modifyng data, not only for getting it. – Michał Turczyn Jan 31 '20 at 09:56
  • 1
    Query string parameters, that's what it's called. I wondered what it was called but it never really appeared in a context where it would click in my head. Something to more deeply look in to. I mostly got suck with the Form Post method because of my lack of technical vocabulary concerning this topic. I thank you for your input. – Intekin Jan 31 '20 at 10:13