【问题标题】:MVC crud operations with paging and retaining the same page after the record on 2,3..nth page is being updated在更新第 2,3..n 页上的记录后,具有分页和保留同一页面的 MVC crud 操作
【发布时间】:2018-08-24 07:18:52
【问题描述】:

(使用另一个视图进行编辑)[不是部分视图]。点击保存后...应该会打开同一页面,其中包含已更新的记录而不是第一页。

控制器中的索引方法

public ActionResult Index(int? page)
        {
            StudentDBHandle dbhandle = new StudentDBHandle();
            ModelState.Clear();
            return View(dbhandle.GetStudent().ToList().ToPagedList(page ?? 1,5));
        }

控制器中的编辑方法

// GET:学生/编辑/5

    public ActionResult Edit(int id)
    {
        StudentDBHandle sdb = new StudentDBHandle();
        return View(sdb.GetStudent().Find(smodel => smodel.UserId == id));
    }

    **// POST: Student/Edit/5**


    [HttpPost]
    public ActionResult Edit(int id, StudentModel smodel)
    {
        try
        {
            StudentDBHandle sdb = new StudentDBHandle();
            sdb.UpdateDetails(smodel);
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

我已在索引视图中启用分页:

@Html.PagedListPager(Model, page => Url.Action("Index", new { page }),
    new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded ,DisplayPageCountAndCurrentLocation = true})

我想将页码从编辑视图传递到索引控制器,以便显示我从中选择要更新的记录的同一页面。

当我在索引视图中使用 @{ViewBag.CurrentPage = page } 时,它会显示错误,即页面不会在当前上下文中出现。

帮助纠正我的错误

【问题讨论】:

  • 不相关,但删除 .ToPagedList(...) 之前的 .ToList() - 这会扼杀性能。而你的ModelState.Clear(); 毫无意义
  • 感谢您的反馈。任何相关的解决方案?
  • 为此使用数据表。
  • @KiranJoshi 你能详细说明一下吗
  • @MansiAgarwal 您可以为此使用数据表。数据表提供对表的分页排序和搜索功能。

标签: c# asp.net-mvc model-view-controller crud paging


【解决方案1】:

您可以在调用RedirectToAction 时将参数作为匿名对象传递。您必须将当前页面发布到 Edit 操作,以便您可以传递它。

[HttpPost]
public ActionResult Edit(int id, StudentModel smodel, int? currentPage){
    try {
        StudentDBHandle sdb = new StudentDBHandle();
        sdb.UpdateDetails(smodel);
        return RedirectToAction("Index", "MyController", new { page = currentPage } );
    }
    catch {
        return View();
    }
}

【讨论】:

  • 但是当我在索引视图中提到它时 ViewBag.currentPage = page 它显示错误
  • 我不认为@Html.PagedListPager 使用 ViewBag 中的这个值,不需要设置它。
  • 您提出的解决方案不正确。上一页没有返回,而是产生 url not found 错误
  • "MyController" 必须是具有“Index”操作的控制器的名称。您没有在问题中提及此控制器的名称。此外,“索引”操作必须是 GET 操作才能使重定向起作用。
  • “/”应用程序中的服务器错误。 **从客户端检测到具有潜在危险的 Request.Path 值** 描述:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。在执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪来识别有关异常起源和位置的信息。
猜你喜欢
  • 1970-01-01
  • 2018-01-24
  • 2018-05-04
  • 2015-11-17
  • 2017-11-05
  • 2011-01-05
  • 2016-05-29
相关资源
最近更新 更多