我试图将一个通用列表存储在viewstate支持的属性中,如下所示: [Serializable] public class UploadedFile { public string FileName { get; set; } public Guid FileGuid { get; set; } } public ListUploadedFile UploadedFiles {
[Serializable] public class UploadedFile { public string FileName { get; set; } public Guid FileGuid { get; set; } } public List<UploadedFile> UploadedFiles { get { return (List<UploadedFile>) (ViewState["UploadedFiles"] ?? new List<UploadedFile>()); } set { ViewState["UploadedFiles"] = value; } }
当我尝试将项添加到列表时,UploadedFiles.Count保持为零:
var uploadedFile = new UploadedFile {FileName = args.FileName, FileGuid = args.FileGuid}; UploadedFiles.Add(uploadedFile); // UploadedFiles.Count == 0 here!
有人有想法吗?
当您创建初始列表时,不要将其保存到viewstate中,试试这个..public List<UploadedFile> UploadedFiles { get { var list = (List<UploadedFile>) (ViewState["UploadedFiles"] ?? new List<UploadedFile>()); ViewState["UploadedFiles"] = list; return list; } set { ViewState["UploadedFiles"] = value; } }