C# 如何将对象列表存储到 ViewState 中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13437820/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store list of object into ViewState
提问by user1613523
I have a list of type List<JobSeeker>. I want to store it in ViewState. How this can be done?
我有一个类型列表List<JobSeeker>。我想将它存储在 ViewState 中。如何做到这一点?
private List<JobSeeker> JobSeekersList { get; set; }
采纳答案by Aristos
Basically you only need to use the get, then on get you either get the posted data from view state, or set it for the first time on the view state. This is the more robust code to avoid all the checks on each call (is view state set, exist etc), and direct saved and use the view state object.
基本上你只需要使用get, 然后你要么从视图状态中获取发布的数据,要么在视图状态上第一次设置它。这是更健壮的代码,以避免对每次调用的所有检查(是否设置视图状态、存在等),并直接保存和使用视图状态对象。
// using this const you avoid bugs in mispelling the correct key.
const string cJobSeekerNameConst = "JobSeeker_cnst";
public List<JobSeeker> JobSeekersList
{
    get
    {
        // check if not exist to make new (normally before the post back)
        // and at the same time check that you did not use the same viewstate for other object
        if (!(ViewState[cJobSeekerNameConst] is List<JobSeeker>))
        {
            // need to fix the memory and added to viewstate
            ViewState[cJobSeekerNameConst] = new List<JobSeeker>();
        }
        return (List<JobSeeker>)ViewState[cJobSeekerNameConst];
    }
}
Alternative to avoid the is
避免的替代方法 is
// using this const you avoid bugs in mispelling the correct key.
const string cJobSeekerNameConst = "JobSeeker_cnst";
public List<JobSeeker> JobSeekersList
{
    get
    {
        // If not on the viewstate then add it
        if (ViewState[cJobSeekerNameConst] == null)                
            ViewState[cJobSeekerNameConst] = new List<JobSeeker>();
        // this code is not exist on release, but I check to be sure that I did not 
        //  overwrite this viewstate with a different object.
        Debug.Assert(ViewState[cJobSeekerNameConst] is List<JobSeeker>);
        return (List<JobSeeker>)ViewState[cJobSeekerNameConst];
    }
}
and the JobSeekerclass must be [Serializable]as
和JobSeeker类必须[Serializable]为
[Serializable]
public class JobSeeker
{
    public int ID;
    ...
}
and you simple call it normally as an object and will never be null. Also will be return the saved on viewstate values after the post back
并且您通常将其简单地称为对象,并且永远不会为空。也将在回发后返回保存的视图状态值
JobSeekersList.add(new JobSeeker(){ID=1});
var myID = JobSeekersList[0].ID;
回答by abatishchev
private IList<JobSeeker> JobSeekersList
{
    get
    {
        // to do not break SRP it's better to move check logic out of the getter
        return ViewState["key"] as List<JobSeeker>;
    }
    set
    {
        ViewState["key"] = value;
    }
}

