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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -