模型在jquery ajax发布后没有更新

问题描述 投票:0回答:1

我正在开发一个应该根据选定的值返回多个列表的应用程序。

选择组织后,将运行一些查询来填充选择列表。这些查询仅返回与所选组织相关的数据。

之后会有一个下拉列表来选择应该显示哪个选择列表。这将使用查询返回的数据填充另一个下拉列表。

在初始加载时,它会正确加载到第一个组织。但是,当选择其他组织时,ajax帖子将调用方法。这将成功运行查询并将属性设置为返回的列表。但问题是剃刀页面仍然使用初始加载的数据。

这导致我将选择的任何组织,它将使用剃刀页面上第一个组织的查询数据。

在ajax电话下面

         $.ajax({
                method: "POST",
                url: "/Aanleveringen/Create?handler=Filter",
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("XSRF-TOKEN",
                        $('input:hidden[name="__RequestVerificationToken"]').val());
                },
                dataType: "json",
                data: { organisatieId: $('#ddlOrganisatie option:selected').val() },
                success: function (msg) {
                    alert(msg);
                },
                error: function (req, status, error) {
                    alert("Error try again");
                }
            });

和方法:

    [HttpPost]
    public IActionResult OnPostFilter(int organisatieId)
    {
        Filter filter = new Filter();
        Organisatie organisatie = _context.Organisatie.Distinct().Where(x => x.Id == organisatieId).First();         
        FilterWaardeGemeente = filter.GetFilterGemeente(organisatie);
        FilterWaardeDienst = filter.GetFilterDienst(organisatie);
        FilterWaardeClient = filter.GetFilterClient(organisatie);
        return new JsonResult("Gelukt");
    }

寻找任何建议,使用最新的c#pagemodel更新剃刀页面模型。

提前致谢。

c# jquery ajax asp.net-core
1个回答
0
投票

更改模型不会做任何事情,因为Razor页面没有被重新渲染。由于您是通过AJAX发出请求,因此需要从该操作返回选择列表项,然后使用JavaScript在DOM中手动更新选择列表选项。

© www.soinside.com 2019 - 2024. All rights reserved.