3

I have a multiple roles and I need to pass those roles to a another view via Return RedirectTo Action method,

My Code

string[] roles = new string[] { "Admin", "Supervisor", "Interviewer" };
if (roles.Count() > 1)
{
    return RedirectToAction("LoginAs","Admin",new{@roles = roles});
}

When I pass like this in the LoginAs view shows me Url like this,

http://localhost:33883/Admin/LoginAs?roles=System.String[]

But there's no passed values.

1 Answer 1

6

Option 1 :

    public ActionResult Index()
    {
        string[] roles = new string[] { "Admin", "Supervisor", "Interviewer" };
        var routeParameters = new RouteValueDictionary();
        for (int i = 0; i < roles.Length; i++)
        {
            routeParameters["roles[" + i + "]"] = roles[i];
        }
        return RedirectToAction("Test", "Student", routeParameters);
    }

    public ActionResult Test(string[] roles)
    {
        return View("Index");
    }

Output -

enter image description here

Option 2: Use TempData

    public ActionResult Index()
    {
        string[] roles = new string[] { "Admin", "Supervisor", "Interviewer" };
        TempData["data"] = roles;
        return RedirectToAction("Test", "Student");
    }

    public ActionResult Test()
    {
        string[] roles = (string[])TempData["data"];
        return View("Index");
    }

Output -

enter image description here

Option 3 : Use Session

    public ActionResult Index()
    {
        string[] roles = new string[] { "Admin", "Supervisor", "Interviewer" };
        Session["data"] = roles;
        return RedirectToAction("Test", "Student");
    }

    public ActionResult Test()
    {
        string[] roles = (string[])Session["data"];
        return View("Index");
    }

Output -

enter image description here

3
  • Thanks.What a awesome answer.I used 1st method.
    – UDP
    Commented Apr 25, 2015 at 10:37
  • In the option 1 when i use it, then my URL shows me the rols linst. like : localhost:33883/Home/… , How to avoid that?
    – UDP
    Commented Apr 25, 2015 at 10:51
  • I dont think you can avoid that, thats why i provided Option 2,3.
    – ramiramilu
    Commented Apr 25, 2015 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.