RedirectToAction with parameter

后端 未结 14 1906
傲寒
傲寒 2020-11-22 08:56

I have an action I call from an anchor thusly, Site/Controller/Action/ID where ID is an int.

Later on I need to redirect to th

14条回答
  •  鱼传尺愫
    2020-11-22 09:27

    If one want to Show error message for [httppost] then he/she can try by passing an ID using

    return RedirectToAction("LogIn", "Security", new { @errorId = 1 });
    

    for Details like this

     public ActionResult LogIn(int? errorId)
            {
                if (errorId > 0)
                {
                    ViewBag.Error = "UserName Or Password Invalid !";
                }
                return View();
            }
    
    [Httppost]
    public ActionResult LogIn(FormCollection form)
            {
                string user= form["UserId"];
                string password = form["Password"];
                if (user == "admin" && password == "123")
                {
                   return RedirectToAction("Index", "Admin");
                }
                else
                {
                    return RedirectToAction("LogIn", "Security", new { @errorId = 1 });
                }
    }
    

    Hope it works fine.

提交回复
热议问题