c# - Pass array to a view using query string MVC -
i have multiple roles , need pass roles 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 pass in loginas view shows me url this,
http://localhost:33883/admin/loginas?roles=system.string[]
but there's no passed values.
option 1 :
public actionresult index() { string[] roles = new string[] { "admin", "supervisor", "interviewer" }; var routeparameters = new routevaluedictionary(); (int = 0; < roles.length; i++) { routeparameters["roles[" + + "]"] = roles[i]; } return redirecttoaction("test", "student", routeparameters); } public actionresult test(string[] roles) { return view("index"); }
output -
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 -
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 -
Comments
Post a Comment