In ASP. NET, form elements are converted into Entity objects or collections.

Source: Internet
Author: User

In ASP. NET, form elements are converted into Entity objects or collections.
This article mainly introduces ASP. NET to convert form elements into Entity objects or sets. This article is an encapsulation of repeated data processing. It is a very practical development technique. For more information, see

 

 

Introduction:

WEBFROM developers know that it is very troublesome to receive parameters in the background.

Although the form in MVC can be directly converted into a set, the form conversion to a LIST <T> is not supported.

Usage of a single object:

Form:

 

The Code is as follows:


<Input name = 'id' value = '1'>
<Input name = 'sex' value = 'male'>

 

Background:

The Code is as follows:


// Previous statement
DLC_category d = new DLC_category ();
D. sex = Request ["sex"];
D. id = Convert. ToInt32 (Request ["id"]);

 


// Write now
Var category = RequestToModel. GetSingleForm <DLC_category> ();

 

Collection object usage:

Form:

The Code is as follows:


<Input name = 'id' value = '1'>
<Input name = 'sex' value = 'male'>


<Input name = 'id' value = '2'>
<Input name = 'sex' value = 'female '>

<Input name = 'id' value = '3'>
<Input name = 'sex' value = 'female '>


Background:

The Code is as follows:


List <DLC_category> categoryLists = RequestToModel. GetListByForm <DLC_category> ();

 

Source code:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

Using System;

Using System. Collections. Generic;

Using System. Linq;

Using System. Web;

 

Namespace SyntacticSugar

{

/// <Summary>

/// ** Description: Form help class

/// ** Founding Date:

/// ** Modification time :-

/// ** Prepared by sunkaixuan

/// ** Qq: 610262374 welcome to the exchange, joint improvement, naming syntax, and other poorly written areas. You are welcome to give valuable suggestions.

/// </Summary>

Public class RequestToModel

{

 

/// <Summary>

/// Submit a form to obtain a single image through reflection

/// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned.

/// </Summary>

Public static T GetSingleForm <T> () where T: new ()

{

T t = SetList <T> (null, 0). Single ();

Return t;

}

 

 

/// <Summary>

/// Submit a form to obtain a single image through reflection

/// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned.

/// <Param name = "appstr"> Control prefix. For example, name = "form1.sex" appstr can be set to form1 </param>

/// </Summary>

Public static T GetSingleForm <T> (string appstr) where T: new ()

{

T t = SetList <T> (appstr, 0). Single ();

Return t;

}

 

/// <Summary>

/// Submit a form to obtain multiple objects through reflection

/// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned.

/// </Summary>

/// <Typeparam name = "type"> </typeparam>

/// <Param name = "type"> </param>

/// <Returns> </returns>

Public static List <T> GetListByForm <T> () where T: new ()

{

List <T> t = SetList <T> (null, 0 );

Return t;

}

 

/// <Summary>

/// Submit a form to obtain multiple objects through reflection

/// Note: The Form Control name must contain the first field in the corresponding class. Otherwise, an error is returned.

/// </Summary>

/// <Typeparam name = "type"> </typeparam>

/// <Param name = "appstr"> Control prefix. For example, name = "form1.sex" appstr can be set to form1 </param>

/// <Returns> </returns>

Public static List <T> GetListByForm <T> (string appstr) where T: new ()

{

List <T> t = SetList <T> (appstr, 0 );

Return t;

}

 

 

/// <Summary>

/// Submit a form to obtain multiple objects through reflection

/// </Summary>

/// <Typeparam name = "type"> </typeparam>

/// <Param name = "appstr"> Control prefix. For example, name = "form1.sex" appstr can be set to form1 </param>

/// <Typeparam name = "index"> the first control in the Form Control, which corresponds to the index number of fields in the class. In special cases, it can be the second and third control. </typeparam>

/// <Returns> </returns>

Private static List <T> GetListByForm <T> (string appstr, int index) where T: new ()

{

List <T> t = SetList <T> (appstr, index );

Return t;

}

 

 

 

Private static List <T> SetList <T> (string appendstr, int index) where T: new ()

{

List <T> t = new List <T> ();

Try

{

Var properties = new T (). GetType (). GetProperties ();

Var subNum = System. Web. HttpContext. Current. Request [appendstr + properties [index]. Name]. Split (','). Length;

For (int I = 0; I <subNum; I ++)

{

Var r = properties;

Var model = new T ();

Foreach (var p in properties)

{

String pval = System. Web. HttpContext. Current. Request [appendstr + p. Name + ""];

If (! String. IsNullOrEmpty (pval ))

{

Pval = pval. Split (',') [I];

String pptypeName = p. PropertyType. Name;

P. SetValue (model, Convert. ChangeType (pval, p. PropertyType), null );

}

}

T. Add (model );

}

}

Catch (Exception ex)

{

 

 

Throw ex;

}

 

 

Return t;

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.