【问题标题】:Redirect back to Previous Page without losing original data在不丢失原始数据的情况下重定向回上一页
【发布时间】:2021-05-18 22:53:57
【问题描述】:

当我在控制器中执行自定义错误处理时,我无法通过重定向同一页面来保留原始数据。 假设我有一个网页调用 Create.cshtml。 在那个创建网页中,我有一些表单控件需要用户输入类代码,但类代码不能重复。 假设用户输入了系统中存在的类代码,我的系统应该重定向回 Create.cshtml 并传递错误消息(例如 ViewBag.error = "Class Code duplicated")并同时传递。 但是我当前的实现在重定向后不会恢复原始内容/数据。

类控制器:

  [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("ID,ClassCode,ClassName,DateCreation,DegreeID,CourseChapterID")] Class @class)
        {
            if (ModelState.IsValid)
            {
                Class cls = await _context.Class.SingleOrDefaultAsync(c => c.ClassCode == @class.ClassCode);
                if (cls != null)
                {
                    TempData["error"] = "This class code has been existed in the system";
                ModelState.AddModelError("error", "This class code has been existed in the system");
                return RedirectToAction(nameof(Create),@class);
                }
                _context.Add(@class);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(@class);
        }

创建.cshtml

<form asp-action="Create">
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="ClassCode" class="control-label"></label>
        <input asp-for="ClassCode" class="form-control" />
        <span asp-validation-for="ClassCode" class="text-danger"></span>
    </div>
    <div class="form-group">
        <label asp-for="ClassName" class="control-label"></label>
        <input asp-for="ClassName" class="form-control" />
        <span asp-validation-for="ClassName" class="text-danger"></span>
    </div>
    @if (@TempData["error"] != null)
    {
        <div class="form-group">
            <label class="control-label">@TempData["error"]</label>
        </div>
    }
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>

系统环境:.NET Core Entity Framework

【问题讨论】:

    标签: c# asp.net .net asp.net-core asp.net-mvc-3


    【解决方案1】:

    当你使用重定向时,你应该使用 TempData 而不是 ViewBag。

    TempData["error"] = "This class code has been existed in the system";
    

    return RedirectToAction(nameof(Create));

    你被重定向到createget方法,如果不返回模型,数据会丢失,我想你可以直接在这里return View()

    if (cls != null)
    {
        TempData["error"] = "This class code has been existed in the system";
        ModelState.AddModelError("error", "This class code has been existed in the system");
        return View(nameof(Create), @class);
    }
    

    【讨论】:

    • 但我怀疑如何在视图页面中捕获 ModelState?我试图在视图页面中调用 TempData.error,但它没有返回任何内容。
    • 像这样调用 TempData:TempData["error"]
    • 我认为我不应该使用重定向,因为一旦我重定向,页面本身会刷新页面的所有状态,旧数据不会被持久化。在这种情况下,如何弹回上一页并将ModelState.error发送回上一页。
    • 您可以像我的帖子中那样直接返回带有模型的视图。如果不重定向,您可以使用 ViewBag 或TempData。
    • 我已经更新了主线程中的最新代码,请重新检查。
    猜你喜欢
    • 2016-01-19
    • 2020-03-20
    • 1970-01-01
    • 2023-04-05
    • 2018-09-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 2018-07-15
    相关资源
    最近更新 更多