【问题标题】:How to maintain an Edit URL and existing data in a Save action如何在保存操作中维护编辑 URL 和现有数据
【发布时间】:2014-04-03 12:42:33
【问题描述】:

我有一个 Save 方法,它有一些额外的服务器端验证。如果失败,我想在浏览器中维护“/Edit/id” URL,以便刷新将保留在页面上。 (如果在这种情况下无法做到这一点,请告诉我。)

以下是我现在如何尝试执行此操作的简要视图:

[HttpPost]
public ActionResult Save(UserEditModel model) {
    try {
        repository.SaveUser(model.CopyTo());
    }
    catch (InvalidOperationException ex) {
        // Doing this to just display it at the top of the page as it is not property-specific.
        TempData["UserError"] = ex.Message;
        // Doing this to maintain the "Edit/id" URL.
        return RedirectToAction("Edit", "Users", new { id = model.Id });
    }

    // Want to keep the URL on the Index page as "Users", instead of "Users/Save".
    return RedirectToAction("Index", "Users", new { page = model.Page });
}

除了 URL,我遇到的最大问题是,因为我正在重定向,所以我也会丢失用户在保存尝试失败时已经在字段中输入的所有数据。

我已经尝试将一个可选的UserEditModel 参数添加到默认为 null 的 Edit 操作,但是当从页面上的链接导航到 Edit 操作(而不是从 Save 操作重定向)时,可选的 Model 参数正在获取默认为new UserEditModel(),而不是我在参数列表中给出的默认值null。

【问题讨论】:

    标签: c# asp.net-mvc-4 controller url-redirection


    【解决方案1】:

    您可以使用TempData 将数据放入控制器中。并在您的视图中设置一些条件,如果它不为空,则从视图包中检索数据并将其放回。

    在你的控制器中:

    [HttpPost]
    public ActionResult Save(UserEditModel model) {
    try {
        TempData["Field1"] = model.Field1;
        TempData["Field2"] = model.Field2;
         .....
        repository.SaveUser(model.CopyTo());
    }
    catch (InvalidOperationException ex) {
        // Doing this to just display it at the top of the page as it is not property-specific.
        TempData["UserError"] = ex.Message;
        // Doing this to maintain the "Edit/id" URL.
        return RedirectToAction("Edit", "Users", new { id = model.Id });
    }
    
    // Want to keep the URL on the Index page as "Users", instead of "Users/Save".
    return RedirectToAction("Index", "Users", new { page = model.Page });}
    

    然后在你看来:

      @{if(TempData["Field1"] != null)
        //Assign your textbox here
    
       else
       //your first code to display the textBox
    
        }
    

    希望对你有帮助。

    【讨论】:

    • 你不是说TempData,因为使用重定向,ViewBag 和ViewData 会被清除,对吗?
    • 这是一个不错的想法,我可以看到使用。但是,我会改进它,而不是将属性单独存储在 TempData 中,而是存储整个 Model 对象。然后在页面顶部的 Razor 块中,检查对象是否存在于 TempData 中,如果存在,则在模型上运行 Copy 方法以覆盖所有属性。这样你就不需要每个文本框周围的if/else。
    • 是的。像你说的那样好。
    • 如果你愿意,我会让你再次更新你的答案,并接受它。你得到了所有的功劳。
    • 好的,所以我只是尝试做这样的事情,并意识到我以前没有想到的事情。 Model 在 Razor 页面中是只读的。因此,您需要在离开操作方法之前分配给 Model 对象。我的方法行不通。 ://
    【解决方案2】:

    您应该有一个[HttpGet] 操作,return View(model); 将保存您的数据和网址

    public ActionResult Save(int id)
    { 
        return View(repository.GetUserById(id));
    }
    
    
    [HttpPost]
    public ActionResult Save(UserEditModel model) {
        try {
            repository.SaveUser(model.CopyTo());
            return RedirectToAction("Index", "Users", new { page = model.Page });
        }
        catch (InvalidOperationException ex) {
            TempData["UserError"] = ex.Message; 
            return View(model);
        }
    }
    

    【讨论】:

    • 但是错误之后的 Url 会是 Users/Save,对吗?
    • 它将是 Users/Save/2
    • 对。这就是引发整个事情的问题,导致我使用Redirect 而不仅仅是return Edit()。另外,有了 Save.cshtml,我必须在另一个视图中复制所有代码。
    • 可能我100%不懂你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-02
    • 1970-01-01
    相关资源
    最近更新 更多