Pergunta

Now I am creating application using MVC Music Store tutorial. I want to implement sort function. I got stuck in implementation of Post Browse action.

enter image description here

Browse.cshtml

@model Overstock.Models.Category

@{
    ViewBag.Title = "Browse Albums";
}

<div class="product">
    <h3><em>@Model.Name</em> Products</h3>


<form action="/Store/Browse" method="POST">
    Sort 
    <select id="Category" name="Category">
    <option value="All">All</option>
    <option value="Electronics">Electronics</option>
    <option value="Sports">Sports</option>
    <option value="Watches">Watches</option>
    <option value="Office">Office</option>
    <option value="Beauty">Beauty</option>
    </select>
    products by
    <select id="SortType" name="SortType">
    <option selected="selected" value="Name">Name</option>
    <option value="Price">Price</option>
    </select>
    in
    <select id="OrderBy" name="OrderBy">
    <option selected="selected" value="Ascending">Ascending</option>
    <option value="Descending">Descending</option>
    </select>
    order
    <input type="submit" value="Set" />
</form>

    <ul id="product-list">
        @foreach (var product in Model.Products)
        {
            <li id="product">
                <a href="@Url.Action("Details", new { id = product.ProductId })">
                    <img id="image" alt="@product.Title" src="@product.PictureUrl" />
                    <span>@product.Title</span>
                </a>
            </li>
        }
    </ul>
</div>

StoreController.cs

   // GET: /Store/Browse
        public ActionResult Browse(string category)
        {
            var varCategory = MyDB.Categories.Include("Products").Single(c=> c.Name==category);
            return View(varCategory);
        }

        [HttpPost]
        public ActionResult Browse(string Category, string SortType, string OrderBy)
        {
            //What should I put here?
            return View();
        }

What should I write in Browse Post action? Please help.

Nenhuma solução correta

Outras dicas

Using OrderBy and OrderByDesceding for example:

if(SortType == "Name") && (OrderBy == "Ascending")
   MyDB.Categories.Where(x => x.Name == Category).OrderBy(x => x.Name);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top