'MVC 5 Entity Framework How to Sort with a Partial View

I am having trouble SORTING products by id_desc or by Title on product list page for my Ecommerce site. I have read through examples on how to do this but in my situation I am using a Partial View in my controller to return the products in a grid. In the tutorials I have seen like here they are simply using an ActionResult that return View(students.ToList()) instead of a partial view like me.

In my controller this is what I am returning:

 public PartialViewResult GetProductsAllProds2(int? id, string Title, string sortOrder)
    {

        IQueryable<tblStoreProduct> GetProductsLoadonPLP = this.db.tblStoreProducts;
        GetProductsLoadonPLP = db.tblStoreProducts.Where(x => x.isActive == true);

        ViewBag.TitleSortParm = String.IsNullOrEmpty(sortOrder) ? "Title_desc" : "";
        ViewBag.idSortParm = sortOrder == "id" ? "id_desc" : "id";

        var otblStoreProducts = from s in db.tblStoreProducts
                                select s;
        switch (sortOrder)
        {
            case "Title_desc":
                otblStoreProducts = otblStoreProducts.OrderByDescending(s => s.Title); 
                break;
            case "id":
                otblStoreProducts = otblStoreProducts.OrderBy(s => s.id);
                break;
            case "id_desc":
                otblStoreProducts = otblStoreProducts.OrderByDescending(s => s.id); 
                break;
            default:
                otblStoreProducts = otblStoreProducts.OrderBy(s => s.Title);
                break;
        }

        return PartialView("_partialview1ProdListPageAll", GetProductsLoadonPLP);
    }

Then in my view which is a partial view called _partialview1ProdListPageAll I have this:

@model IEnumerable<MyEcomStore.Models.tblStoreProduct>

@Html.ActionLink("Title", "GetProductsAllProds2", new { sortOrder = ViewBag.TitleSortParm })

@Html.ActionLink("id", "GetProductsAllProds2", new { sortOrder = ViewBag.idSortParm })


 @foreach (var item in Model)
 {
 if (item.isActive == true)
 {



    <div class="col-6 col-sm-6 col-md-4 col-lg-3 item">
        <div class="product-image">
            <a href="~/product/@item.id/@item.slug">
                <div class="card-group">
                    <div class="card" style="border-color:#d3cccc; border-radius: 5px; 
                         padding-bottom: 5px;">
                        <img src="@item.ProductImage" class="card-img-top" alt="...">
                        <div class="card-body">
                            <div id="limitText">
                                <p class="card-text" style="font-size: 15px;">@item.Title </p>
                                <hr />
                                @*<br />*@
                            </div>
     
                        </div>
                    </div>

                </div>
                </div>

                </div>

When I run the project and click on the 2 links for those view bags I expect the url to add the parameter of ?sortOrder=id_desc to the end of the url if we were to for example click on the link for the id. It should look like this below AND all the products on the grid should be ordered by largest id to smallest id. My url does not end up looking like that and the products order stays the same even if I manually type that into the url. http://localhost:5000/product/amazing-products?sortOrder=id_desc

Instead the URL looks like this: http://localhost:50000/product/GetProductsAllProds2?sortOrder=id

It is taking the name of my public PartialViewResult from the controller and adding it to the url. Also even if I were to manually enter in the URL the exact url that I want, it still does not even sort the products by id descending or by title alphabetical order. None of the products on the grid switch positions..

What am I doing wrong ? How can I make this work in my situation where I am using @model IEnumerable<MyEcomStore.Models.tblStoreProduct> on the view to load a partial view instead of an actual list like Microsoft example in the link above?

This is the tech I am using: Entity Framework with MVC5 asp.net C#



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source