C# ASP.NET MVC显示成功消息

C# ASP.NET MVC显示成功消息,c#,asp.net-mvc,C#,Asp.net Mvc,下面是我从应用程序中删除记录的示例方法: [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.SaveChang

下面是我从应用程序中删除记录的示例方法:

[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");
}
我想做的是在索引视图上显示一条消息,上面写着:“Lorem ipsum文章已被删除”我该怎么做?谢谢

以下是我当前的索引方法,以防万一:

    // 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);
    }
//索引
[手机错误]
公共操作结果索引(字符串查询,int?页)
{
//构建查询
var ArticleQuery=从_db.ArticleSet中选择一个;
//检查其是否为查询
如果(!string.IsNullOrEmpty(查询))
{
ArticleQuery=ArticleQuery.Where(a=>a.headline.Contains(query));
//msp 2011-01-13您需要使用ViewData将查询字符串发送到视图
ViewData[“查询”]=查询;
}
//先订购最新的商品
var OrderedArticles=ArticleQuery.OrderByDescending(a=>a.posted);
//获取已订购的文章并使用PaginatedList类对其进行分页,每页4个
var PaginatedArticles=新的分页列表(OrderedArticles,第0页,第4页);
//将分页的文章返回到视图
返回视图(分页);
}

一种方法是使用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");
}
索引
操作中,您可以从TempData获取此消息并加以利用。例如,您可以将其作为视图模型的属性传递,该属性将传递给视图,以便它可以显示:

public ActionResult Index()
{
    var message = TempData["message"];
    // TODO: do something with the message like pass to the view
}

更新:

例如:

public class MyViewModel
{
    public string Message { get; set; }
}
然后:

public ActionResult Index()
{
    var model = new MyViewModel
    {
        Message = TempData["message"] as string;
    };
    return View(model);
}
在强类型视图中:

<div><%: Model.Message %></div>


好的,听起来很有趣。1.)如何将其传递到视图中。2.)然后如何在视图中显示它。谢谢:)您使用字符串属性定义了一个ViewModel,该属性将分配给此邮件。然后,将视图强式键入此视图模型,并在视图中使用
。或者您在控制器操作
ViewData[“message”]=message中执行了一些非常丑陋的操作和视图内部:
(我重复一遍,这很难看,我不推荐,ViewData很差劲)。我不太理解ViewModel,也不太了解它。你能在上面的代码中给出一个例子吗?谢谢。这个放在哪里?在我的控制器里面
public类MyViewModel{public string Message{get;set;}}
而且我的索引已经有了一个返回视图,那么如何将消息传递给它呢?我已经在我的原始问题中展示了我的索引方法。谢谢。我创建了一个nuget软件包,它可以帮助从controller发送(错误、警告、信息和成功)消息,以查看已准备好引导的: