带有错误消息的RedirecttoAction [英] RedirecttoAction with error message

查看:106
本文介绍了带有错误消息的RedirecttoAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AdminUsers视图中的网格上有一个链接

I have a link on a grid in my AdminUsers view

grid.Column(header: "", format: (item) => (condition ? Html.ActionLink("Impersonate", "Impersonate", "Admin", new { id = item.username }, null) : Html.Label("Impersonate"), style: "webgrid-column-link"),

在控制器中,我有

public ActionResult Impersonate(string id)
{
    string result = ORCA.utilities.users.setImpersonation(id);
    if(result == "nocommonfields")
        return RedirectToAction("AdminUsers", "Admin");
    else
        return RedirectToAction("terms_of_use", "Forms");
}

当我返回AdminUsers页面时,如何发送一条错误消息以显示?

How can send an error message to display when I return to the AdminUsers page?

推荐答案

您可以使用 TempData

if(result == "nocommonfields")
{
    TempData["ErrorMessage"]="This is the message";
    return RedirectToAction("AdminUsers", "Admin");
}

,并且在AdminUsers操作中,您可以阅读

and in your AdminUsers action, you can read it

public ActionResult AdminUsers()
{
  var errMsg=TempData["ErrorMessage"] as string;
 //check errMsg value do whatever you want now as needed
}

请记住,TempData的寿命很短.会话是临时数据背后的备份存储.

Remember, TempData has very short-life span. Session is the backup storage behind temp data.

或者,您也可以考虑在查询字符串中发送一个标志,并在下一个操作方法中读取它,然后决定显示什么错误消息.

Alternatively, You may also consider sending a flag in your querystring and read it in your next action method and decide what error message to show.

这篇关于带有错误消息的RedirecttoAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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