1

In my C# app I am showing order list in a table. The url to that page is

/Organization/Orders/id

id in this case is organization id. My controller looks like this

    [HttpGet]
    public ActionResult Orders(String id)
    {
        Organization org = Organization.Get(id);
        ViewBag.Orders = org.GetOrders(50).ToList();
        return View(org);
    }

Now I am trying to implement a search functionality in there. In the view I have a search form going on. When the user hits the search button I take him to this controller

    [HttpPost]
    public ActionResult Orders(String orgId, OrderSearch os)
    {
        Organization org = Organization.Get(orgId);
        ViewBag.Orders = org.GetOrders(50).Where(i => i.GetLocation().FriendlyName.Contains(os.FriendlyName)).ToList();
        return View(org);
    }

The issue with this approach is that, if the user search for a particular location name it will take him to the Post method. It will work fine but lets assume that the user refreshes the browser it will still show the searched content rather than the reset the search.

I understand it is because I am in the post method and in the post parameters I would have locationName.

So, is there any other good way to do implement search?

mohsinali1317
  • 4,255
  • 9
  • 46
  • 85
  • Make your form a GET, and change the method to `[HttpGet]` –  Oct 16 '15 at 08:22
  • 1
    You may find [this approach](http://stackoverflow.com/a/33154580/3110834) helpful. – Reza Aghaei Oct 16 '15 at 08:23
  • 1
    It won't work, I have tried that. The issue is that it be ambiguous between the two functions. Which one to call. – mohsinali1317 Oct 16 '15 at 08:24
  • @RezaAghaei how should I be calling it from client? I mean what would be the form look like? – mohsinali1317 Oct 16 '15 at 08:26
  • Using a form you can post/get the request to that action like other actions. – Reza Aghaei Oct 16 '15 at 08:28
  • 2
    The `foreach` loop is not actually needed, you can do `orders = org.GetOrders(50).Where(i => i.GetLocation().FriendlyName.Contains(locationName)).ToList();` – DavidG Oct 16 '15 at 08:30
  • @RezaAghaei the issue that I still would have is where would be the "orgId" go? The parameter I am passing to get organization? – mohsinali1317 Oct 16 '15 at 08:31
  • It would be in `OrganizationSearchModel`. – Reza Aghaei Oct 16 '15 at 08:33
  • @RezaAghaei Not necessarily needed, the URL could simply change to `/Organization/Orders/id?locationName=xxx` or perhaps with a routing change `/Organization/Orders/id/xxx` – DavidG Oct 16 '15 at 08:36
  • Can I please get a code snippet? – mohsinali1317 Oct 16 '15 at 08:37
  • @DavidG the solution doesn't have any conflict with routing.It simply uses `Model Binding` and works with each `Route` you prefer to use. It also works with `GET`and `POST` and have no limit working with both of them. – Reza Aghaei Oct 16 '15 at 08:42
  • @RezaAghaei I know, I was just pointing out that you don't need a view model just for this, it can be done entirely over the URL. – DavidG Oct 16 '15 at 08:43
  • @RezaAghaei now I am using the search model that you mentioned. The issue is still the same. I am on a page getting list of all the orders. I then search for a particular location. I get the redefined result list. Now I refresh the browser but I still get the filtered result. I still have two actions one with searchModel other the simple one. Will edit my question so that you can see. – mohsinali1317 Oct 16 '15 at 08:47
  • @DavidG Putting all values in url or in a post is not related to that model. The model is for getting data from view and passing to business logic. Also using that model is only a part of that solution to have an organized application :) – Reza Aghaei Oct 16 '15 at 08:49
  • @ChaudhryMohsinAli Using that approach makes your application more clean and more flexible and answers **So, is there any other good way to do implement search?** – Reza Aghaei Oct 16 '15 at 08:50
  • @ChaudhryMohsinAli But about refresh problem. When you refresh a page using **f5** in case of **using `Get`** obviously search values will deliver to your action, because they are in url. When you refresh a page using **f5** in case of using **using `Post`** browser doesn't automatically send post parameters. – Reza Aghaei Oct 16 '15 at 08:56
  • @RezaAghaei thanks for the better approach. My code is much cleaner now. But the issue I am having is with the refresh one. My second action is Post and still I get those parameters. – mohsinali1317 Oct 16 '15 at 09:06
  • Dear @ChaudhryMohsinAli did you read my previous comment? What you expect from **f5**? – Reza Aghaei Oct 16 '15 at 09:10
  • @RezaAghaei now I get. :) – mohsinali1317 Oct 16 '15 at 09:11

0 Answers0