c# - 使用 ASp.net MVC3 插入数据库后如何显示类似 "successfully Inserted"的警告消息

标签 c# asp.net asp.net-mvc-3 entity-framework-4

如何在用户数据存储到数据库后,使用 MVC 编写代码来显示警报消息:“已成功注册”

我正在使用 Asp.Net MVC3、C#、实体模型。

最佳答案

尝试使用 TempData:

public ActionResult Create(FormCollection collection) {
  ...
  TempData["notice"] = "Successfully registered";
  return RedirectToAction("Index");
  ...
}

然后,在您的索引 View 或母版页等中,您可以这样做:

<% if (TempData["notice"] != null) { %>
  <p><%= Html.Encode(TempData["notice"]) %></p>
<% } %>

或者,在 Razor View 中:

@if (TempData["notice"] != null) {
  <p>@TempData["notice"]</p>
}

引自 MSDN(页面自 2014 年起不再存在,存档副本 here):

An action method can store data in the controller's TempDataDictionary object before it calls the controller's RedirectToAction method to invoke the next action. The TempData property value is stored in session state. Any action method that is called after the TempDataDictionary value is set can get values from the object and then process or display them. The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables scenarios such as redirection, because the values in TempData are available beyond a single request.

关于c# - 使用 ASp.net MVC3 插入数据库后如何显示类似 "successfully Inserted"的警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8063345/

相关文章:

c# - 如何通过电子邮件正文需要支持 HTML 的 SMTP 保留换行符

asp.net - asp.net ObjectDataSource错误处理

asp.net - 带有 %3D 的 URL 链接的 IIS URL 重写

C# 文本框行距

c dll 的 C# .net 包装器,特别是 lglcd (g19 sdk)

c# - StructureMap 可以返回特例吗?

c# - 后台代理中的静态变量值不同

c# - MVC Html数据属性渲染区别

c# - 存储来自1个ActionResult的js值以在另一个ActionResult中使用

asp.net-mvc - asp.net mvc 嵌套 View 模型表单提交