如何将此场景保持一次往返?

时间:2022-04-01 21:01:24

I have a page that is a list of users. The controller's Index function is the action responsible for showing the page of users. The user can select those users and chose to delete them.

我有一个用户列表的页面。控制器的索引功能是负责显示用户页面的操作。用户可以选择这些用户并选择删除它们。

I perform the delete action with an ajax request, however then the page's list of users is out of date. So I reload the page because I want to re-use the index action, and all the query string parameters are still there. This means I'm performing two round trips. How do I avoid this?

我使用ajax请求执行删除操作,但是页面的用户列表已过期。所以我重新加载页面,因为我想重新使用索引操作,并且所有查询字符串参数仍然存在。这意味着我正在进行两次往返。我该如何避免这种情况?

function DeleteUsers()
{
    var selectedUserIds = ;
    $.post("/Account/DeleteUsers",
    { 
        userIds: selectedUserIds
    },
    function (data) {
        if( data.status == "success"){
            location.reload();
        }
    });
}

Index function:

    [AuthorizeActionFilter]
    public ActionResult Index(UserModel model)
    {
        ViewData["PageTitle"] = ServiceSite.Resources.Resources.REGISTERED_USERS;
        ViewBag.MaxUsersPerPage = PAGE_MAX_COUNT;

        if(model  == null)
        {
            model = new UserModel();
        }

        int totalCount = 0;

        //Get users
        model.Users = CADC.GetUsers(AccountController.GetRegionID(), model.CompanyID, out totalCount, 
            model.SortField, model.PageNumber * PAGE_MAX_COUNT, PAGE_MAX_COUNT, model.Ascending, User.Identity.Name);

        model.TotalUserCount = totalCount;

        int totalPages = totalCount / PAGE_MAX_COUNT;
        model.TotalPages = (totalCount % PAGE_MAX_COUNT) == 0 ? totalPages : totalPages + 1;

        return View(model);
    }

and the model:

和模型:

public class UserModel
{
   public bool Ascending { get; set; }

    public int PageNumber { get; set; }

    public string SortField { get; set; }

    public int TotalUserCount { get; set; }

    public int TotalPages { get; set; }

    public long CompanyID { get; set; }

    public List<User> Users { get; set; }

    public UserModel()
    {
        this.Ascending = true;
        this.PageNumber = 0;
        this.SortField = "FirstName";
        this.CompanyID = 0;
        this.TotalUserCount = 0;
        this.TotalPages = 0;
    }

}

1 个解决方案

#1


0  

I agree with @David's comments. In the AJAX success block, remove the rows just deleted based on what you requested to delete.

我同意@ David的评论。在AJAX成功块中,根据您请求删除的内容删除刚刚删除的行。

The pagination is indeed more complicated; either adjust it in the same way, or revamp everything to use a proper view model in Javascript with bound elements--see AngularJS or other such frameworks.

分页确实更复杂;或者以相同的方式调整它,或者修改所有内容以在Javascript中使用绑定元素的正确视图模型 - 请参阅AngularJS或其他此类框架。

#1


0  

I agree with @David's comments. In the AJAX success block, remove the rows just deleted based on what you requested to delete.

我同意@ David的评论。在AJAX成功块中,根据您请求删除的内容删除刚刚删除的行。

The pagination is indeed more complicated; either adjust it in the same way, or revamp everything to use a proper view model in Javascript with bound elements--see AngularJS or other such frameworks.

分页确实更复杂;或者以相同的方式调整它,或者修改所有内容以在Javascript中使用绑定元素的正确视图模型 - 请参阅AngularJS或其他此类框架。