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

相关内容

热门资讯

2024教程!pokermas... 2024教程!pokermaster脚本,wepoker有透视底牌吗,爆料教程(有挂技巧);1.we...
带你了解!wepoker透视脚... 带你了解!wepoker透视脚本免费使用视频,hhpoker有辅助吗,解密教程(有挂透明);科技安装...
专业讨论!聚星ai辅助工具收费... 专业讨论!聚星ai辅助工具收费多少,we poker插件,介绍教程(有挂技巧);玩家必备必赢加哟《1...
最新研发!wepoker国外版... 最新研发!wepoker国外版透视,wepoker怎么挂底牌,插件教程(有挂技巧);原来确实真的有挂...
重大通报!werplan脚本,... 重大通报!werplan脚本,hhpoker底牌透视脚本,安装教程(有挂软件)准备好在hhpoker...
必备攻略!红龙poker辅助平... 必备攻略!红龙poker辅助平台,红龙poker作弊指令,必备教程(有挂技巧);大神普及一款德州ai...
重大消息!hhpoker万能辅... 重大消息!hhpoker万能辅助器,哈糖大菠萝怎么开挂,德州论坛(有挂透明)是一款可以让一直输的玩家...
让我来分享经验!hhpoker... 您好,hhpoker是内部控制吗这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】...
我来教教大家!德扑之心免费透视... 我来教教大家!德扑之心免费透视,aapoker万能辅助器,切实教程(有挂教程);大神普及一款德州ai...
我来向大家传授!pokerno... 我来向大家传授!pokernow辅助工具,wepoker手机插件,专业教程(有挂攻略);玩家必备必赢...
Name Category Price
@product.Name