c# - Wrong list pasted via RedirectToAction method in ASP.NET MVC -


i faced following error while using redirecttoaction method in asp.net mvc 5.

    [httppost]     public actionresult uploadorderreport(httppostedfilebase file)     {         string targetfolder = httpcontext.server.mappath("~/reports");         string targetpath = path.combine(targetfolder, file.filename);         file.saveas(targetpath);          var currentreports = directory.getfiles(targetfolder).tolist();         return redirecttoaction("currentprofile", new { existreport = new list<string>(currentfiles)});     } 

however, in currentprofile method got unexpected data

[httpget] public actionresult index(list<string> existreports) 

and debugger shows existsreports argument system.collections.generic.list``1[system.string]

i suppose problem related type casting?

redirecttoaction method going return 302 response browser new url location header value. browser make new http request new url.

if @ redirecttoaction method overloads, can see third parameter routevalues(which querystring values)

protected internal redirecttorouteresult redirecttoaction(     string actionname,     string controllername,     object routevalues ) 

you should not passing complex data(like list of items) redirecttoaction method. basically, when pass small objects,it converted querystring values.

if want pass complex data, suggest pass uniqueid list can retrieved in action again. if not option, consider using tempdata.

passing unique id

return redirecttoaction("currentprofile",new { listid="someuniqueidhere"}); 

this set location header value /yourcontroller/currentprofile?listid=someuniqueidhere

passing via tempdata

tempdata["existreport "] = new list<string>(currentfiles); return redirecttoaction("currentprofile", "yourcontrollername"); 

and read in currentprofile action method again.

public actionresult currentprofile() {         var items=tempdata["existreport"] list<string>   return view(model); } 

take @ how include model redirecttoaction? well


Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -