我正在编写一个代码来处理一个表单,该表单包含多个从用户收集信息的页面。它在ASP.NET w / C#中。 所以我认为不是使用Post在页面之间传递,而是使用Session来存储它们。
我的流程是每个页面从其上一页获取Post值,并立即将它们分配到相同标识符下的相应Session变量中。 是否有一种简单的方法来处理它而不是手动分配每个变量?
一些代码将不胜感激。
答案 0 :(得分:0)
您可以创建一个(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