【问题标题】:Use Ajax to update the value of two dropdowns filters C# MVC使用 Ajax 更新两个下拉列表过滤器的值 C# MVC
【发布时间】:2020-04-25 23:00:32
【问题描述】:

我已经开发了一个 mvc Web 应用程序,其中我有一个包含数据的表,我可以使用两个下拉列表进行过滤(一个我可以选择数据的值状态,另一个我可以选择数据的值是否关闭 - 是/否)我可以同时选择它们,如果我按下按钮提交过滤它就可以了。现在我想实现一些 ajax 调用,比如当我选择一个下拉列表的值时会自动更新结果表,但我无法将下拉列表的值传递给控制器​​,谁能帮助我,谢谢

<table  id="tb2">    
    <tr>
        <th>
            <h4> LIST : @Html.DropDownList("stato", "ALL ")</h4>
        </th>
        <th>
            <h4>ARCHVIED : @Html.DropDownList("closed", "ALL ")</h4>
        </th>
        <th>
            <input type="submit" value="Filter" class="btn btn-info" />
        </th>
    </tr>
</table>

这是控制器:

public ActionResult Filter(string stato,  string closed)
    {
        List<Card> cards = new List<Card>();

        List<Closed> closedList = new List<Closed>();
        closedList.Add(new Closed("False"));
        closedList.Add(new Closed("True"));
        ViewBag.stato = new SelectList(myApi.GetState(), "Name", "Name");
        ViewBag.closed = new SelectList(closedList, "Id", "Name");

        if ((stato != null && stato != "") || (closed != null && closed != ""))
        {
            foreach (var card in model)
            {
                if (card.IdList == stato || stato == "")
                {
                    if (card.Closed == closed || closed == "")
                    {
                        cards.Add(card);
                    }
                }
            }
            return View(cards);
        }
        return View(model);
    }

这是我的 jquery

jQuery(document).ready(function ($) {
var drpdown1 = $("#Dropdown1Id");
var drpdown2 = $("#Dropdown1Id");
drpdown1.on('change', function () {
    $.ajax({
        type: "GET",
        url: " /Select/Filter",
        data: { stato: drpdown1.val(), closed: drpdown2.val() },
        success: function (res) {
            var dropValue1 = drpdown1.val();
            var dropValue2 = drpdown2.val();
            stato = dropValue1;
            closed = dropValue2;
        }
    })
})
});

但它不起作用,我是 ajax 新手,需要一些帮助

【问题讨论】:

  • 请添加您的 jQuery 代码。
  • 在这里我们想帮助解决问题。请提供您的尝试。您可以使用本指南“我如何提出一个好问题?”来改进您的问题。这里:stackoverflow.com/help/how-to-ask
  • 我已经更新了我的问题,请问您是否正确?

标签: c# jquery asp.net ajax model-view-controller


【解决方案1】:

更改您的 html 给出按钮单击的方法。为两个下拉菜单提供 ID,然后编写如下 ajax 函数

function YourMethod() {
        var drpdown1= $("#Dropdown1Id").val();
        var drpdown2= $("#Dropdown2Id").val();
        $.ajax({
            type: "GET",
            url: "/ControllerName/Filter",
            data: { stato: drpdown1,closed:drpdown2},
            success: function(res) {
               //Your work here                      
            }
        })
    }

【讨论】:

  • 我想删除提交按钮,只使用下拉菜单的更改值
  • 然后只需在下拉列表中添加 onChange YourMethod
猜你喜欢
  • 2015-09-08
  • 1970-01-01
  • 1970-01-01
  • 2015-06-27
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 2018-04-28
  • 2021-01-09
相关资源
最近更新 更多