如何将值从枚举选择列表传递给控制器​​以过滤结果

时间:2019-06-17 22:08:20

标签: c# razor asp.net-core-mvc

我已经在网页上创建了一个下拉菜单,该菜单需要按国家/地区过滤从数据库加载的结果。我已经从枚举类创建并填充了列表,我将在下面的代码中提供该列表。我一直想弄清楚如何将选择的值传递给控制器​​,以便执行过滤器?

//View
@model List<Merchandise>

@{
    ViewData["Title"] = "Variant Summary";
}

<header>
    <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
        <div class="container">
            <h2>Variant Summary</h2>
            @Html.DropDownList("Countries", new SelectList(Enum.GetValues(typeof(Countries))))             
        </div>
    </nav>
</header>

//Model
 public class Merchandise
    {
        public string Id { get; set; }
        //g.PlanSeqId, g.ItemId, g.ItemDescription, g.OrderQuantity, g.OrderSldTdy, g.PlannedMinutesQty, g.ActualMinutesQty
        public int PlanSeqId { get; set; }
        public int ItemId { get; set; }
        public string ItemDescription { get; set; }
        public int OrderQuantity { get; set; }
        public int OrderSldTdy { get; set; }
        public int PlannedMinutesQty { get; set; }
        public int ActualMinutesQty { get; set; }
        public Countries Countries { get; set; }
    }

    public enum Countries
    {
        USA, EUR, JPN
    }

//Controller
public IActionResult Merchandise()
        {            
            Countries selCountry = Countries.USA;
            switch (selCountry)
            {
                case Countries.USA:
                    var bucket = _bucketProvider.GetBucket("MerchUSA");
                    var n1ql = @"SELECT META(g).id, g.PlanSeqId, g.ItemId, g.ItemDescription, g.OrderQuantity, g.OrderSldTdy, g.PlannedMinutesQty, g.ActualMinutesQty
                                FROM `MerchUSA` g
                                WHERE g.CompanyId = 0;
                                LIMIT = 20;";
                    var query = QueryRequest.Create(n1ql);
                    var results = bucket.Query<Merchandise>(query);
                    return View(results.Rows);
                case Countries.EUR:
                    var bucket2 = _bucketProvider.GetBucket("MerchEUR");
                    var n1ql2 = @"SELECT META(g).id, g.PlanSeqId, g.ItemId, g.ItemDescription, g.OrderQuantity, g.OrderSldTdy, g.PlannedMinutesQty, g.ActualMinutesQty
                                FROM `MerchEUR` g
                                WHERE g.CompanyId = 1;
                                LIMIT = 20;";
                    var query2 = QueryRequest.Create(n1ql2);
                    var results2 = bucket2.Query<Merchandise>(query2);
                    return View(results2.Rows);
                case Countries.JPN:
                    var bucket3 = _bucketProvider.GetBucket("MerchJPN");
                    var n1ql3 = @"SELECT META(g).id, g.PlanSeqId, g.ItemId, g.ItemDescription, g.OrderQuantity, g.OrderSldTdy, g.PlannedMinutesQty, g.ActualMinutesQty
                                FROM `MerchJPN` g
                                WHERE g.CompanyId = 2;
                                LIMIT = 20;";
                    var query3 = QueryRequest.Create(n1ql3);
                    var results3 = bucket3.Query<Merchandise>(query3);
                    return View(results3.Rows);
            }
            return View();
        }

现在我只看到这些国家/地区的下拉列表,但是没有值传递给控制器​​来过滤结果。

1 个答案:

答案 0 :(得分:0)

正如Ammar所说,您应该通过request传递dropdownlist值。尝试使用ajax调用Merchandise方法并以querystring格式发送所选值。

查看

@Html.DropDownList("Countries", new SelectList(Enum.GetValues(typeof(Countries))),new  { @id="dropdownlist"})

@section Scripts
{
  <script type="text/javascript">
    $("#dropdownlist").change(function () {
        var select = $("#dropdownlist").val();
        $.ajax({
            type: "get",
            url: "/YourController/Merchandise?select=" + select,
            success: function () {
                alert("ok");
            }
        });
    });
  </script>
}

控制器

public async Task<IActionResult> GetAccommodation(Countries select)
{
        switch (select)
        {
            case Countries.USA:
               // the stuff you want
        }
 }