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) {

相关内容

热门资讯

透视总结!aapoker能控制... 透视总结!aapoker能控制牌吗(透视)插件(切实有挂)1、操作简单,无需注册,只需要使用手机进行...
透视数据!竞技联盟辅助(透视)... 透视数据!竞技联盟辅助(透视)教你攻略(2021已更新)(哔哩哔哩)亲,关键说明,竞技联盟辅助赛季回...
辅助透视!aapoker公共底... 辅助透视!aapoker公共底牌(透视)辅助(好像存在有挂)1、让任何用户在无需aapoker公共底...
透视有挂!佛手在线大菠萝辅助(... 透视有挂!佛手在线大菠萝辅助(透视)科技教程(2026已更新)(哔哩哔哩);1、该软件可以轻松地帮助...
透视教程!aapoker怎么提... 透视教程!aapoker怎么提高中牌率(透视)透视脚本(切实有挂)1、玩家可以在aapoker怎么提...
透视黑科技!fishpoker... 透视黑科技!fishpoker透视底牌(透视)2025教程(2021已更新)(哔哩哔哩);1、构建自...
透视教程!红龙poker辅助平... 透视教程!红龙poker辅助平台(透视)详细教程(2025已更新)(哔哩哔哩);1、金币登录送、破产...
透视系统!aapoker透视脚... 透视系统!aapoker透视脚本入口(透视)插件(一直真的是有挂);1、操作简单,无需注册,只需要使...
透视辅助!epoker透视底牌... 透视辅助!epoker透视底牌(透视)可靠教程(2021已更新)(哔哩哔哩)透视辅助!epoker透...
透视游戏!aapoker辅助插... 透视游戏!aapoker辅助插件工具(透视)ai插件(本来真的有挂);1、aapoker辅助插件工具...
Name Category Price
@product.Name