ASP.NET MVC显示成功消息 [英] ASP.NET MVC Show success message
问题描述
下面是一个例子方法,我有一个删除从我的应用程序的记录:
[授权(角色=新闻管理)]
公众的ActionResult删除(INT ID)
{
VAR ArticleToDelete =(来自于哪里_db.ArticleSet == a.storyId ID选择).FirstOrDefault();
_db.DeleteObject(ArticleToDelete);
_db.SaveChanges(); 返回RedirectToAction(「指数」);
}
我想这样做是显示说像在索引视图一条消息:Lorem存有文章已被删除我会怎么做呢?谢谢
下面是我目前的指数方法,以防万一:
// INDEX
[的HandleError]
公众的ActionResult指数(查询字符串,诠释?页)
{
//构建查询
在_db.ArticleSet VAR ArticleQuery =从选择;
//检查,如果他们是一个查询
如果(!string.IsNullOrEmpty(查询))
{
ArticleQuery = ArticleQuery.Where(一个= GT; a.headline.Contains(查询));
// MSP 2011-01-13您需要查询字符串发送到使用的ViewData查看
计算机[查询] =查询;
}
//通过最新第一批订单的文章
变种OrderedArticles = ArticleQuery.OrderByDescending(一个= GT; a.posted);
//需要有序的文章,并使用PaginatedList类进行分页他们4每页
变种PaginatedArticles =新PaginatedList<物品>(OrderedArticles,页δλ0,4);
//分页的文章返回视图
返回查看(PaginatedArticles);
}
的一种方法是使用TempData的:
[授权(角色=新闻管理)]
公众的ActionResult删除(INT ID)
{
VAR ArticleToDelete =(来自于哪里_db.ArticleSet == a.storyId ID选择).FirstOrDefault();
_db.DeleteObject(ArticleToDelete);
_db.SaveChanges();
TempData的[消息] =Lorem存有文章已被删除;
返回RedirectToAction(「指数」);
}
和索引中的
的行动,你可以获取从TempData的此消息,并利用它。例如,你可以把它作为将被传递到您的视图视图模型的属性,以便它可以表现出来:
公众的ActionResult指数()
{
VAR消息= TempData的[消息];
// TODO:做一些像传递给视图消息
}
更新:
例如:
公共类MyViewModel
{
公共字符串消息{搞定;组; }
}
和则:
公众的ActionResult指数()
{
VAR模型=新MyViewModel
{
消息= TempData的[消息]作为字符串;
};
返回查看(模型);
}
和强类型视图中:
< DIV><%:Model.Message%GT;< / DIV>
Here is an example method I have that deletes a record from my app:
[Authorize(Roles = "news-admin")]
public ActionResult Delete(int id)
{
var ArticleToDelete = (from a in _db.ArticleSet where a.storyId == id select a).FirstOrDefault();
_db.DeleteObject(ArticleToDelete);
_db.SaveChanges();
return RedirectToAction("Index");
}
What I would like to do is show a message on the Index view that says something like: "Lorem ipsum article has been deleted" how would I do this? Thanks
Here is my current Index method, just in case:
// INDEX
[HandleError]
public ActionResult Index(string query, int? page)
{
// build the query
var ArticleQuery = from a in _db.ArticleSet select a;
// check if their is a query
if (!string.IsNullOrEmpty(query))
{
ArticleQuery = ArticleQuery.Where(a => a.headline.Contains(query));
//msp 2011-01-13 You need to send the query string to the View using ViewData
ViewData["query"] = query;
}
// orders the articles by newest first
var OrderedArticles = ArticleQuery.OrderByDescending(a => a.posted);
// takes the ordered articles and paginates them using the PaginatedList class with 4 per page
var PaginatedArticles = new PaginatedList<Article>(OrderedArticles, page ?? 0, 4);
// return the paginated articles to the view
return View(PaginatedArticles);
}
One way would be to use TempData:
[Authorize(Roles = "news-admin")]
public ActionResult Delete(int id)
{
var ArticleToDelete = (from a in _db.ArticleSet where a.storyId == id select a).FirstOrDefault();
_db.DeleteObject(ArticleToDelete);
_db.SaveChanges();
TempData["message"] = ""Lorem ipsum article has been deleted";
return RedirectToAction("Index");
}
and inside the Index
action you could fetch this message from TempData and make use of it. For example you could pass it as a property of your view model which will be passed to the view so that it can show it:
public ActionResult Index()
{
var message = TempData["message"];
// TODO: do something with the message like pass to the view
}
UPDATE:
Example:
public class MyViewModel
{
public string Message { get; set; }
}
and then:
public ActionResult Index()
{
var model = new MyViewModel
{
Message = TempData["message"] as string;
};
return View(model);
}
and inside the strongly typed view:
<div><%: Model.Message %></div>
这篇关于ASP.NET MVC显示成功消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!