This article combines Asp.net MVC and wf4.0 to implement a simple multi-step registration for users. User Registration is divided into four steps. The flowchart is as follows:
Step 1: Enter personal information:
Step 2: Fill in the position information:
Step 3: Enter the education level information:
Step 4: Enter the contact information:
Step 5: complete
Wf4.0 state machine such:
Click Next in each step to go to the next step. Click Back to return to the previous step.
Implementation:
Step 1: Create an ASP. net mvc application and a workflow activitydesignerlibrary project, and add a user under the model folder of the MVC project. The Code is as follows:
1 public class user
2 {// personal information
3 [required (errormessage = "name cannot be blank")]
4 [stringlength (20, errormessage = "Name Length cannot exceed 20 characters")]
5 Public string name {Get; set ;}
6 Public Int? Age {Get; set ;}
7 // job information
8 [required (errormessage = "The position cannot be blank")]
9 Public String post {Get; set ;}
10 public Int? Salary {Get; set ;}
11 // educational qualifications
12 [required (errormessage = "the number of graduates cannot be blank")]
13 Public String University {Get; set ;}
14 public Int? Graduationyear {Get; set ;}
15 // contact information
16 [required (errormessage = "email cannot be blank")]
17 [regularexpression (@ "^ [A-Z] [A-z | 0-9 |] * ([_] [A-z | 0-9] +) * ([.] [A-z | "+ @" 0-9] + ([_] [A-z | 0-9] + )*)? @ [A-Z] [A-z | 0-9 |] * \. ([A-Z] "+ @" [A-z | 0-9] * (\. [A-Z] [A-z | 0-9] *)?) $ ", Errormessage =" Incorrect email format ")]
18 Public String email {Get; set ;}
19 public Int? Mobile {Get; set ;}
20}
21
Step 2: Add a control class to controllers. The Code is as follows:
1 public class usercontroller: Controller
2 {
3 //
4 // get:/user/
5 # region declarations
6 static workflowutil wrkflw = NULL;
7 string page = "Step1 ";
8 public user userobj;
9 # endregion
10 # region Process Action Method
11 Public actionresult process (string nextbutton, string backbutton)
12 {
13 if (wrkflw = NULL)
14 {
15 wrkflw = new workflowutil ();
16}
17 if (nextbutton! = NULL ))
18 {
19 page = wrkflw. runworkflow ("Next ");
20 return view (page, userobj );
21}
22 else if (backbutton! = NULL)
23 {
24 modelstate. Clear ();
25 page = wrkflw. runworkflow ("Prev ");
26 return view (page, userobj );
27}
28 else
29 return view (page, userobj );
30}
31 # endregion
32 # region events
33 protected override void onactionexecuting (actionexecutingcontext filtercontext)
34 {
35 userobj = (User) tempdata ["user"];
36 IF (userobj = NULL) userobj = new user ();
37 tryupdatemodel (userobj );
38 If (tempdata ["currentpage"]! = NULL) page = tempdata ["currentpage"]. tostring ();
39}
40 protected override void onresultexecuted (resultexecutedcontext filtercontext)
41 {
42 tempdata ["user"] = userobj;
43 tempdata ["currentpage"] = page;
44}
45 # endregion
46 Public actionresult index ()
47 {
48 return view ();
49}
50}
Step 3: Select Add view on method process, as shown in the following figure:
In this way, five view pages are created: Step 1, step 2, step 3, step 4, step 5, and final.
Step 4: design the state machine workflow. Here, we will only demonstrate Step 1 settings, for example:
Summary:Use WF perfectly with Asp.net MVC to implement this function.
Code:/Files/zhuqil/mvc.rar