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

相关内容

热门资讯

透视系统!wepoker透视脚... 透视系统!wepoker透视脚本免费app,固有是有挂(透视)解密教程(有挂规律)亲,关键说明,we...
透视规律!wepokerplu... 透视规律!wepokerplus外挂,先前是真的有挂(透视)可靠教程(有挂解说);小薇(透视辅助)致...
透视黑科技!werplan外卦... 透视黑科技!werplan外卦神器,来玩app 德州 辅助,攻略教程(有挂细节)1、玩家可以在来玩a...
透视模拟器!佛手在线大菠萝为什... 透视模拟器!佛手在线大菠萝为什么都输,pokemmo手机脚本,实用技巧(有挂规律)1、游戏颠覆性的策...
透视软件!poker辅助器免费... 透视软件!poker辅助器免费安装,原生是真的有挂(透视)软件教程(有挂攻略)1、poker辅助器免...
透视神器!xpoker辅助怎么... 透视神器!xpoker辅助怎么用,pokemmo辅助官网,安装教程(有挂攻略)1、很好的工具软件,可...
透视好友房!wepoker游戏... 透视好友房!wepoker游戏的安装教程,原生真的是有挂(透视)细节揭秘(有挂方法)1、打开软件启动...
透视软件!拱趴大菠萝作弊方法,... 透视软件!拱趴大菠萝作弊方法,xpoker辅助工具,曝光教程(有挂脚本);透视软件!拱趴大菠萝作弊方...
透视辅助!wpk真吗,原生是有... 透视辅助!wpk真吗,原生是有挂(透视)必赢教程(有挂工具)1、wpk真吗机器人多个强度级别选择2、...
透视智能ai!佛手大菠萝13道... 透视智能ai!佛手大菠萝13道挂哪里,智星德州插件最新版本更新内容详解,插件教程(有挂黑科技);1、...
Name Category Price
@product.Name