如何显示警报消息,如“成功插入”使用ASp.net MVC3插入数据库后 [英] How to show Alert Message like "successfully Inserted" after inserting to DB using ASp.net MVC3

查看:85
本文介绍了如何显示警报消息,如“成功插入”使用ASp.net MVC3插入数据库后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to write a code for displaying the alert message: "Successfully registered", after user data is stored in database, using MVC

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

I am using Asp.Net MVC3, C#, Entity Model.

推荐答案

尝试使用 TempData / p>

Try using TempData:

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

然后,在您的索引视图中,或主页面等,你可以这样做:

Then, in your Index view, or master page, etc., you can do this:

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

或者在剃刀视图中:

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

从MSDN引用(截至2014年,已存档的副本 here ):

Quote from MSDN (page no longer exists as of 2014, archived copy here):


一个操作方法可以在调用控制器的RedirectToAction方法来调用下一个操作之前将数据存储在控制器的TempDataDictionary对象中。 TempData属性值存储在会话状态。在TempDataDictionary值之后调用的任何操作方法都可以从对象获取值,然后处理或显示它们。 TempData的值保持不变,直到它被读取或直到会话超时为止。由于TempData中的值在单个请求之外可用,因此以这种方式持久化TempData可以实现重定向等场景。

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.

这篇关于如何显示警报消息,如“成功插入”使用ASp.net MVC3插入数据库后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆