【问题标题】:ASP.NET MVC: After Delete Action, Index is not renderingASP.NET MVC:删除操作后,索引未呈现
【发布时间】:2023-03-15 20:06:01
【问题描述】:

我有一个控制器,它有AddEditDeleteIndex 操作方法。对于我的 Add 操作,在添加之后,它会重定向到 Index 以刷新网格以显示添加。

对于Delete,它不会重定向。正在删除,但只有在手动刷新页面时才能看到它。我试过了

return RedirectToAction(nameof(Index));  

w("Index", otList); 

其中“otList”是模型。

这是在控制器中调用删除操作的代码

    $("#deleteButton").click(function () {
            var button = $(this);
            $.ajax({ method: 'POST', url: button.data('url') })
                .done(function () {
                    $('#confirmModal').modal('hide');
                    button.removeData('url');
                })
                .fail(function () {
                    magalert.communicationError();
                });
        });

这是控制器中的Delete操作方法

    public IActionResult Delete(int Id)
    {
        try
        {
            var jurisdictionId = _user.Current().JurisdictionId;
            _electedOfficials.deleteOrgType(jurisdictionId, Id);

            //Refresh data post delete for the grid
            List<OrganizationType> otList = new List<OrganizationType>();
            otList = (List<OrganizationType>)_electedOfficials.OrgTypeList(jurisdictionId);

            //return View(otList);

            //return RedirectToAction(nameof(Index));

            return View("Index", otList);
        }
        catch (Exception e)
        {
            _logger?.LogCritical(new EventId(103, "CAdminOrganizationType"), e, $"Error when deleting id: " + Id);
            throw;
        }
    }

任何想法为什么Index 视图不像Add 操作之后那样令人耳目一新?我尝试重定向并尝试在返回中指定视图和模型。

如果有帮助,这里是 Index 操作方法

    public IActionResult Index()
    {
        try
        {
            var jurisdictionId = _user.Current().JurisdictionId;
            List<OrganizationType> otList = new List<OrganizationType>();
            otList = (List<OrganizationType>)_electedOfficials.OrgTypeList(jurisdictionId);
            return View(otList);
        }
        catch (Exception e)
        {
            _logger?.LogCritical(new EventId(101, "CAdminOrganizationType"), e, $"Error when loading Organization Types View");
            throw;
        }
    }

【问题讨论】:

  • 您正在通过 AJAX 调用删除,但还希望屏幕重新加载?这没有多大意义。是要使用 AJAX,还是要重新加载页面?
  • 我看不到这里使用 ajax 的原因。为什么不直接提交或引用控制器上的 Delete 方法?

标签: c# ajax asp.net-mvc


【解决方案1】:

如果您使用 Ajax 调用删除,则无法执行重定向,解决此问题的一种方法是使用 location.reload(); 从 javascript 代码刷新页面当在 button.RemoveData('url'); 之后请求成功时 当然,如果从索引页执行删除。

【讨论】:

  • 谢谢,成功了
猜你喜欢
  • 2015-06-10
  • 1970-01-01
  • 2013-07-08
  • 1970-01-01
  • 1970-01-01
  • 2017-11-02
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多