ASP.NETMVC中通过类别下拉列表搜索
创始人
2024-09-19 10:00:39
0
  1. 首先,在Model中定义类别属性及对应的搜索方法:
public class Product {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public decimal Price { get; set; }
}

public class ProductSearchRepository {
    private List products = new List {
        new Product { Id = 1, Name = "iPhone", Category = "Electronics", Price = 999.00m },
        new Product { Id = 2, Name = "Samsung TV", Category = "Electronics", Price = 799.00m },
        new Product { Id = 3, Name = "Nike Shoes", Category = "Clothing", Price = 69.99m },
        new Product { Id = 4, Name = "Levis Jeans", Category = "Clothing", Price = 59.99m },
    };

    public IEnumerable GetCategories() {
        return products.Select(p => p.Category).Distinct().OrderBy(c => c);
    }

    public IEnumerable SearchByCategory(string category) {
        return products.Where(p => p.Category == category).OrderBy(p => p.Name);
    }
}
  1. 在Controller中创建类别下拉列表:
public class ProductsController : Controller {
    private ProductSearchRepository repository = new ProductSearchRepository();

    public ActionResult Index(string category) {
        ViewBag.Category = new SelectList(repository.GetCategories());

        var products = string.IsNullOrEmpty(category) ? new List() : repository.SearchByCategory(category);
        return View(products);
    }
}
  1. 在View中添加类别下拉列表:
@model IEnumerable

@using (Html.BeginForm()) {
    

@Html.Label("Category:") @Html.DropDownList("category", (IEnumerable) ViewBag.Category, "All Categories", new { onchange = "this.form.submit();" })

} @foreach (var product in Model) {

相关内容

热门资讯

透视有挂(wepoke辅助挂)... 透视有挂(wepoke辅助挂)微扑克软件发牌原理(详细辅助科技教程)先前是有挂,您好,wepoke辅...
透视透视(wePOke)透明挂... 透视透视(wePOke)透明挂辅助工具(wepoke有没有挂)一贯是真的有挂(详细透视解密教程)是一...
透视黑科技(德州ai辅助有用)... 透视黑科技(德州ai辅助有用)cloudpoker辅助器(详细辅助普及教程)固有真的有挂;人气非常高...
透视规律(wePokE)透视辅... 透视规律(wePokE)透视辅助工具(WePoKe透视挂)果然真的是有挂(详细透视技巧教程);实战中...
透视透视(智星德州菠萝)wep... 透视透视(智星德州菠萝)wepower系统规律(详细辅助必胜教程)起初真的有挂1、许多玩家不知道智星...
透视存在(WepokE)外挂透... 透视存在(WepokE)外挂透明挂辅助软件(wepoke一定有挂)从前存在有挂(详细透视爆料教程);...
透视代打(微扑克ai机器人)w... 透视代打(微扑克ai机器人)wepokewopoker透明挂(详细辅助攻略教程)一向有挂1、微扑克a...
透视线上(wepOKE)透视辅... 此外,数据分析德州(wepokeai辅助)辅助神器app还具备辅助透视行为开挂功能,通过对客户wep...
透视工具(wepoke有挂)a... 透视工具(wepoke有挂)aa poker智能ai辅助介绍(详细辅助切实教程)一向是真的有挂,您好...
透视ai代打(wepOkE)外... 《透视ai代打(wepOkE)外挂透明挂辅助神器(wepoke辅助挂)素来真的有挂(详细透视黑科技教...
Name Category Price
@product.Name