RedirecttoAction with error message

后端 未结 2 1376
孤街浪徒
孤街浪徒 2020-12-29 11:33

I have a link on a grid in my AdminUsers view

grid.Column(header: \"\", format: (item) => (condition ? Html.ActionLink(\"Impersonate\", \"Impersonate\", \         


        
2条回答
  •  隐瞒了意图╮
    2020-12-29 11:59

    You may use TempData

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

    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
    }
    

    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.

提交回复
热议问题