質問

I have a form with multiple steps. When the user finishes the first step, a session is created.

On every step, except the first one, I am using the following check to determine whether a session has been created:

if(Session["form_data"] == null)
{
     throw new ArgumentException("Session not created!");
}

This works fine if the user has not started filling in a form. However, the user can navigate to, say, step 5 if he filled in step 1. This is due to the fact that a session is created in step 1 and therefore my check would fail.

How can I get around this? How can I ensure that the user does not skip form steps by typing in the URL directly in the address bar once he filled in step 1?

役に立ちましたか?

解決

You can assign some value to Session and increment it on each step instead of just checking that if session exists.

if(Session["form_data"] == null && Session["form_data"].ToString() == "2") //2 is for second step
{
     throw new ArgumentException("Session not created!");
}

For first step

Session["form_data"] = "1";

For second step

Session["form_data"] = "2";

So on...

他のヒント

You could store another value in the session to indicate which "step" you are on.

if(Session["Step"].ToString() != "1")
{
     throw new ArgumentException("Step is not correct");
}

You could create an session array. initialize it with null values. each index can be each form stage. once a stage is complete assign that index a true value. on load of each form you can check if any of the previous indexes are null eg if the user on form 3 then check if form 2 or 1 is null.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top