代码之家  ›  专栏  ›  技术社区  ›  smdrager

在MVC2中向用户传递消息

  •  2
  • smdrager  · 技术社区  · 16 年前

    我正在使用ASP.NET MVC2进行我的项目。我想在操作后发送用户确认消息。

    理想的: 用户单击带有查询字符串的链接(即删除条目的链接)

    有人建议我使用一个模型错误来做这件事,但我不认为这将在这种情况下工作。

    谢谢。

    3 回复  |  直到 16 年前
        1
  •  7
  •   Darin Dimitrov    16 年前

    你可以用 TempData :

    public ActionResult Index()
    {
        string message = TempData["message"] as string ?? string.Empty;
        // send the message as model so that the view can print it out
        return View("index", message);
    }
    
    [HttpPost]
    public ActionResult DoWork()
    {
        // do some work
        TempData["message"] = "Work done!";
        return RedirectToAction("index");
    }
    

    内部 临时数据

        2
  •  2
  •   joshperry    16 年前

        3
  •  2
  •   Carles Company    16 年前

    我将TempData用于我的Site.Master文件中的消息:

      <% if (TempData["Error"] != null)
         { %>
    
        <div id="errorMessage">
            <%= Html.Encode(TempData["Error"]) %>
        </div>
       <% } %>
    
       <% if (TempData["Warning"] != null)
          { %>
    
        <div id="warningMessage">
            <%= Html.Encode(TempData["Warning"]) %>
        </div>
       <% } %>
    

    在我的控制器中,我可以给任何一个 TempData["Error"] 或者 TempData["Warning"]