代码之家  ›  专栏  ›  技术社区  ›  Wouter Vanherck

ASP.NET:将消息从控制器传递到视图(ViewBag、ViewData或TempData)

  •  0
  • Wouter Vanherck  · 技术社区  · 8 年前

    情况

    在我的asp.net项目中,我有一个允许用户更改密码的视图。提交表单后,用户将使用 RedirectToAction . 在这里,我想通知用户密码更改是否成功(计划使用 Alertify JS )然而,我似乎无法成功地将消息从控制器传递到我的视图。

    如何在使用时正确地将数据(简单字符串)从控制器传递到视图 重定向操作 不使用 ViewBag , ViewData , Tempdata ,……?或者,如果有办法用这些来解决我目前的困难,很高兴听到。

    1. 小精灵 / 可视数据 :返回后无法使用 重定向操作 我不能把它传给url而不是 View . 我考虑过用 Ajax 归还 JSON 而不是使用 重定向操作 它能让我使用 小精灵 但这带来了一些困难,会改变项目的整体结构。

    2. TempData :我没有 session 在“我的”中配置 Startup.cs 因为我错过了 Microsoft.AspNetCore.Session 命名空间。我也不能添加名称空间,因为这会增加一些复杂性。请参阅以下错误: System.TypeLoadException: 'Method 'ConfigureAppConfiguration' in type 'Microsoft.AspNetCore.Hosting.WebHostBuilder' from assembly 'Microsoft.AspNetCore.Hosting, Version=1.0.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.'

    我不想和饼干或者其他的现金系统打交道。
    编辑: 我也不想使用querystring,但是看到评论/答案让我意识到我的选择是有限的。我忘了在前面的列表中添加querystring/route参数。

    managecontroller.cs

    [HttpGet]
    public IActionResult UserDetail(int id)
    {
        // Code to GET the UserViewModel
        return View(model);
    }
    
    [HttpGet]
    public IActionResult UserPassword(int id)
    {
        // Code to GET the UserViewModel
        return View(model);
    }
    
    [HttpPost]
    public IActionResult UserPassword(UserViewModel model)
    {
        // Code to check and save the password.
        if (user.Password == model.Password) {
            //ViewData["Message"] = "Succeeded";
            //TempData["Message"] = "Succeeded";
        } else {
            //ViewData["Message"] = "Failed";
            //TempData["Message"] = "Failed";
        }
        // TODO: Alert the user
        return RedirectToAction("UserDetail", new { id = model.UserId });
    }
    

    userdetail.cshtml

    <!-- Some view code -->
    <form>
        <button class="ui button button-back" type="button" onclick="window.location.href = '@Url.Action("UserPassword", "Manage", new { id = Model.UserId })';">Change Password</button>
    </form>
    <!-- Trying to use ViewData -->
    @*<div id="id-message" data-value="@ViewData["Message"]"></div>*@
    
    <script>
      $(document).ready(function () {
        console.log("Ready");
    
        // Alert the user of a password change
        //var message = $("#id-message").attr("data-value");
        //var message = "@(TempData["Message"] as string)";
        if (message !== null && message !== "") {
          alertify
            .alertmessage, function () {
              alertify.message('OK');
            };
        }
      });
      // Some script code
    </script>
    
    3 回复  |  直到 8 年前
        1
  •  1
  •   Satpal    8 年前

    可以将其作为路由参数传递 RedirectToAction() 方法

    return RedirectToAction("UserDetail", new { id = model.UserId, message= "" });
    

    接受它

    [HttpGet]
    public IActionResult UserDetail(int id, string message)
    {
       // Code to GET the UserViewModel
       return View(model);
    }
    
        2
  •  0
  •   Just Fair    8 年前

    关于您描述的其他选项,我建议sessionstate:

    string passwordChange = "Password has been changed";
    Session["yourName"] = passwordChange  // inside your if else logic calling it from your UserDetail controller method. 
    

    或者创建一个jsonresult结果来引用ajax调用。

        3
  •  0
  •   Ibrahim Malluf    8 年前

    变化:

    [HttpGet]
    public IActionResult UserDetail(int id)
    

    到

    [HttpPost]
    public IActionResult UserDetail(UserViewModel model)
    

    将消息属性添加到模型中。

    应该为你做的