我正在编写一个代码来处理一个表单,该表单由从用户那里收集信息的多页组成。它位于带有 C# 的 ASP.NET 中。所以我想不是使用 Post 在页面之间传递,而是使用 Session 来存储它们。
我的流程是每个页面都从其前一页面获取 Post 值,并立即将它们分配到相同标识符下的相应 Session 变量中。有没有一种简单的方法来处理它而不是手动分配每个变量?
一些代码将不胜感激。
您可以创建一个(POCO)类,在该类上设置您的值,然后将该类置于会话中。然后在下一页从会话中获取该类并根据需要进行修改......并对所有页面执行此操作。
(编辑)简单示例:
// store this class in a file in your app_code folder called something like MyClass.cs
public class MyClass
{
public string SomeStringValue;
public int SomeIntValue;
public List<SomeOtherClass> MyClassCollection = new List<SomeOtherClass>();
}
public class SomeOtherClass
{
public string OtherStringValue;
}
// At the beginning of your form, on your first page, instance this class
MyClass oMyClass = new MyClass();
// Then, when you collect the data on the page, set the values in the class
oMyClass.SomeStringValue = "derp";
// Then, store it on session in that page
Session["oMyClass"] = oMyClass;
// Then, on the next page, get the object from session
MyClass oMyClass = (MyClass)Session["oMyclass"];
// Then, set the next value in your object
oMyClass.SomeIntValue = -1;
// Then, place it back in session on that page, and then keep chuggin through your workflow.
// Now, I didn't show you how to use MyClassCollection that is in the MyClass object, but it would be simple for you to store a class from each page in that collection like so
SomeOtherClass oSomeOtherClass = new SomeOtherClass();
oSomeOtherClass.OtherStringValue = "herp";
oMyclass.MyClassCollection.Add(oSomeOtherClass);
// Then you can place that collection in session