c# - TempData value implicitly cast from List<string> to string[] after RedirectToAction() -


i using tempdata allow error message displayed after redirect. compatibility, using list<string> add messages list of errors in notificationhelper class:

if (tempdata["errornotification"] == null) {     tempdata["errornotification"] = new list<string> {         message     }; } else {     var data = (list<string>) tempdata["errornotification"];     data.add(message);     tempdata["errornotification"] = data; } 

inside controller, fill tempdata error message , before return redirecttoaction() call, tempdata contains dictionary record list<string> (to exact: system.collections.generic.list`1[system.string]) value right after call, inside terms() function, value becomes array (system.string[]).

[httpget] public iactionresult terms() {     return view(); }  [httpget] public iactionresult index() {     notificationhelper.adderrornotification("error!", tempdata);     return redirecttoaction("terms"); } 

the issue in order use data, casting type in view so:

var errormessages = tempdata["errornotification"] list<string>;

and after conversion have instead:

var errormessages = tempdata["errornotification"] string[];

one of above casts come null, because of expecting wrong type (depending on if try render view before or after redirect). desired behavior of tempdata? how can make sure view can expect 1 specific type being provided?

serialize list of strings before assigning tempdata if want persist type, , deserialize on retrieval. that's ended doing. though in case ended storing list of notifyentry instead of list of string.

public class notifyentry {     public notifytype type { get; set; }     public string text { get; set; } }  public enum notifytype {     success,     information,     warning,     error } 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -