如何在用户数据存储到数据库后,使用 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/