简体   繁体   English

ASP.NET 核心 MVC 过滤:如何将多个值发送给操作?

[英]ASP.NET Core MVC filtering: How can I send multiple value to action?

I want to filter products in my e-commerce project, but I have a problem: I want my filtering to change dynamically, I choose a brand from the selected list and the necessary filtering is done.我想在我的电子商务项目中过滤产品,但我有一个问题:我希望我的过滤动态变化,我从所选列表中选择一个品牌,然后完成必要的过滤。 When the second filter is done, my first filter is reset and the last filtering becomes valid.当第二个过滤器完成后,我的第一个过滤器被重置,最后一个过滤器生效。

在此处输入图像描述

I need such a URL structure:我需要这样一个 URL 结构:

/?SelectedVendor=Asus&SelectedMemory=4+GB

FilterViewModel.cs FilterViewModel.cs

public class FilterViewModel
    {
        public List<string> Vendors { get; set; }
        public List<string> Memories { get; set; }
        public string SelectedVendor { get; set; }
        public string SelectedMemory { get; set; }
    }

CategoryController.cs类别控制器.cs

    [HttpGet]
    public ViewResult Gaming(int productPage = 1, FilterViewModel model=null)
    {
        
        ProductListViewModel productList = new ProductListViewModel()
        {
            Products =
                _productService.GetProductsByCategoryId(1).OrderBy(p => p.Id).Skip((productPage - 1) * pageSize).Take(pageSize),
            FilterTypes = new FilterViewModel()
            {
                Vendors = _productService.Products.Select(I => I.Vendor).Distinct().OrderBy(I => I).ToList(),
                Memories = _productService.Products.Select(I => I.MemoryCapacity).Distinct().OrderBy(I => I).ToList(),
            }
        };


        if (!string.IsNullOrEmpty(model.SelectedVendor))
        {
            productList.Products = productList.Products.Where(I => I.Vendor.Contains(model.SelectedVendor));
            productList.FilterTypes.Memories = productList.Products.Select(I => I.MemoryCapacity).Distinct()
                .OrderBy(I => I).ToList();
        }

        if (!string.IsNullOrEmpty(model.SelectedMemory))
        {
            productList.Products = productList.Products.Where(I => I.MemoryCapacity == model.SelectedMemory);
        }

        return View(productList);
    }

FilterPartial.cshtml FilterPartial.cshtml

<div class="col-md-3">
        <div class="filter filter-first">
            <h6 class="font-weight-bold">Brand</h6>

            <form method="get" asp-controller="@controllerName" asp-action="@actionName">

                @foreach (var vendor in Model.Vendors)
                {
                    <div class="mt-2 mb-2 pl-2">
                        <input type="submit" value="@vendor" asp-for="SelectedVendor"/>
                    </div>
                }


                <h6 class="font-weight-bold">Memory</h6>
                @foreach (var memory in Model.Memories)
                {
                    <div class="mt-2 mb-2 pl-2">
                        <input type="submit" asp-for="SelectedMemory" value="@memory"/>
                    </div>
                }
            </form>

        </div>
    </div>

Here is a demo to pass both SelectedVendor and SelectedMemory to action:这是将 SelectedVendor 和 SelectedMemory 都传递给操作的演示:

TestFilter.cshtml:测试过滤器.cshtml:

@await Html.PartialAsync("Partial", Model.FilterTypes)
<script>
    function addSelectedVendor(i) {
        $("#SelectedVendor").val($(i).val());
        $("#form1").submit();
    }
    function addSelectedMemory(i) {
        $("#SelectedMemory").val($(i).val());
        $("#form1").submit();
    }
    </script>

Partial.cshtml:部分.cshtml:

<form id="form1" method="get" asp-controller="Test" asp-action="TestFilter">

    @foreach (var vendor in Model.Vendors)
    {
        <div class="mt-2 mb-2 pl-2">
            <input type="button" value="@vendor" onclick="addSelectedVendor(this)" />
        </div>
    }


    <h6 class="font-weight-bold">Memory</h6>
    @foreach (var memory in Model.Memories)
    {
        <div class="mt-2 mb-2 pl-2">
            <input type="button" value="@memory" onclick="addSelectedMemory(this)" />
        </div>
    }
    <input hidden asp-for="SelectedVendor"/>
    <input hidden asp-for="SelectedMemory"/>
</form>

TestController(I use fake data to test): TestController(我用假数据来测试):

 [HttpGet]
            public IActionResult TestFilter(FilterViewModel model)
            {
                ProductListViewModel productList = new ProductListViewModel {  FilterTypes=new FilterViewModel()};
                productList.FilterTypes.Vendors = new List<string> { "Apple", "Asus", "Dell", "Lenovo", "MSI" };
                productList.FilterTypes.Memories = new List<string> { "16GB", "4GB", "8GB"};
    
                return View(productList);
            }

result:结果: 在此处输入图像描述

You can filter by list in your get method.您可以在 get 方法中按列表进行过滤。

you should change get method like that你应该像那样改变get方法

   public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = DbSet;

        if (filter != null) query = query.Where(filter);

        if (includeProperties != null)
            foreach (var includeProperty in includeProperties.Split
                (new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                query = query.Include(includeProperty);


        if (orderBy != null)
            return orderBy(query).ToList();
        return query.ToList();
    }

I want to filter products in my e-commerce project, but I have a problem: I want my filtering to change dynamically, I choose a brand from the selected list and the necessary filtering is done.我想在我的电子商务项目中过滤产品,但我有一个问题:我希望我的过滤动态改变,我从选定的列表中选择一个品牌并完成必要的过滤。 When the second filter is done, my first filter is reset and the last filtering becomes valid.当第二个过滤器完成后,我的第一个过滤器被重置,最后一个过滤器变得有效。

在此处输入图像描述

I need such a URL structure:我需要这样一个 URL 结构:

/?SelectedVendor=Asus&SelectedMemory=4+GB

FilterViewModel.cs FilterViewModel.cs

public class FilterViewModel
    {
        public List<string> Vendors { get; set; }
        public List<string> Memories { get; set; }
        public string SelectedVendor { get; set; }
        public string SelectedMemory { get; set; }
    }

CategoryController.cs类别控制器.cs

    [HttpGet]
    public ViewResult Gaming(int productPage = 1, FilterViewModel model=null)
    {
        
        ProductListViewModel productList = new ProductListViewModel()
        {
            Products =
                _productService.GetProductsByCategoryId(1).OrderBy(p => p.Id).Skip((productPage - 1) * pageSize).Take(pageSize),
            FilterTypes = new FilterViewModel()
            {
                Vendors = _productService.Products.Select(I => I.Vendor).Distinct().OrderBy(I => I).ToList(),
                Memories = _productService.Products.Select(I => I.MemoryCapacity).Distinct().OrderBy(I => I).ToList(),
            }
        };


        if (!string.IsNullOrEmpty(model.SelectedVendor))
        {
            productList.Products = productList.Products.Where(I => I.Vendor.Contains(model.SelectedVendor));
            productList.FilterTypes.Memories = productList.Products.Select(I => I.MemoryCapacity).Distinct()
                .OrderBy(I => I).ToList();
        }

        if (!string.IsNullOrEmpty(model.SelectedMemory))
        {
            productList.Products = productList.Products.Where(I => I.MemoryCapacity == model.SelectedMemory);
        }

        return View(productList);
    }

FilterPartial.cshtml FilterPartial.cshtml

<div class="col-md-3">
        <div class="filter filter-first">
            <h6 class="font-weight-bold">Brand</h6>

            <form method="get" asp-controller="@controllerName" asp-action="@actionName">

                @foreach (var vendor in Model.Vendors)
                {
                    <div class="mt-2 mb-2 pl-2">
                        <input type="submit" value="@vendor" asp-for="SelectedVendor"/>
                    </div>
                }


                <h6 class="font-weight-bold">Memory</h6>
                @foreach (var memory in Model.Memories)
                {
                    <div class="mt-2 mb-2 pl-2">
                        <input type="submit" asp-for="SelectedMemory" value="@memory"/>
                    </div>
                }
            </form>

        </div>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 ASP.NET MVC 中执行控制器后如何将数据发送到操作过滤器? - How Can I Send Data to the Action Filter after Execution of the controller in ASP.NET MVC? 使用 post 方法向 asp.net 核心 3 mvc 动作发送多个参数 - send multiple parameters to asp.net core 3 mvc action using post method 如何在 ASP.NET Core 3.0 中使用 ValidationAttribute 验证多个操作参数 - How can I validate multiple action parameters with ValidationAttribute in ASP.NET Core 3.0 如何中止 ASP.NET MVC 中的操作 - How can I abort an action in ASP.NET MVC 如何在 ASP.NET MVC Core 中执行多路由约束? - How can I do a multiple route constraints in ASP.NET MVC Core? 如何在 ASP.NET Core MVC 中添加不同的多行数据? - How can I add different multiple row data in ASP.NET Core MVC? ASP.NET Core MVC中的过滤和排序 - Filtering and Sorting in ASP.NET Core MVC 如何从ASP.NET MVC中的SQL Server数据库向多个收件人发送电子邮件? - How can I send email to multiple recipients from SQL Server database in ASP.NET MVC? 我如何在asp.net mvc中发送确认电子邮件 - How can I send a Confirmation Email in asp.net mvc 我如何使用jQuery在asp.net MVC中添加/发送多个记录? - How can i add/send multiple records in asp.net MVC using jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM