Convert form elements to entity objects or collections-asp.net C #

Source: Internet
Author: User

Brief introduction:

Do webfrom development of the students know the background receive parameters very troublesome

Although the form can be converted directly to set in MVC, it does not support the collection of forms to list<t>

The use of a single object:

Form:

<input name= ' id '  value= ' 1 ' ><input name= ' sex '  value= ' man ' >

Background:

            // previous wording            New dlc_category ();             = request["sex"];             = Convert.ToInt32 (request["ID"]);             // now the wording            var category = Requesttomodel.getsingleform<dlc_category> ();

Use of Collection objects:

Form:

<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= ' women ' >

  

Background:

  list<dlc_category> categorylists = requesttomodel.getlistbyform<dlc_category> ();

Source:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespacesyntacticsugar{/// <summary>    ///* * Description: Form helper class///* * Founding Date: 2015-4-17///* * Modification time:-///* * Sunkaixuan///* * qq:610262374 welcome communication, common improvement, naming grammar and other bad places welcome you to give valuable advice/// </summary>     Public classRequesttomodel {/// <summary>        ///submitting a form to get a single image by reflection///Note: The form control name must contain the first field in the corresponding class, otherwise an error will be added or a load/// </summary>         Public StaticT getsingleform<t> ()whereT:New() {T T= Setlist<t> (NULL,0).            Single (); returnT; }        /// <summary>        ///submitting a form to get a single image by reflection///Note: The form control name must contain the first field in the corresponding class, otherwise an error will be added or a load/// </summary>         Public StaticT getsingleform<t> (stringAPPSTR)whereT:New() {T T= Setlist<t> (Appstr,0).            Single (); returnT; }        /// <summary>        ///The submission form obtains multiple pairs of images through reflection///Note: The form control name must contain the first field in the corresponding class, otherwise an error will be added or a load/// </summary>        /// <typeparam name= "type" ></typeparam>        /// <param name= "type" ></param>        /// <returns></returns>         Public StaticList<t> getlistbyform<t> ()whereT:New() {List<T> T = setlist<t> (NULL,0); returnT; }        /// <summary>        ///The submission form obtains multiple pairs of images through reflection///Note: The form control name must contain the first field in the corresponding class, otherwise an error will be added or a load/// </summary>        /// <typeparam name= "type" ></typeparam>        /// <param name= "Appstr" >control prefixes, such as name= "Form1.sex" Appstr can be set to Form1</param>        /// <returns></returns>         Public StaticList<t> getlistbyform<t> (stringAPPSTR)whereT:New() {List<T> T = setlist<t> (Appstr,0); returnT; }        /// <summary>        ///The submission form obtains multiple pairs of images through reflection/// </summary>        /// <typeparam name= "type" ></typeparam>        /// <param name= "Appstr" >control prefixes, such as name= "Form1.sex" Appstr can be set to Form1</param>        /// <typeparam name= "index" >The first control in the form control that corresponds to the index number of the field in the class in the class, and the special case can be the second third control</typeparam>        /// <returns></returns>        Private StaticList<t> getlistbyform<t> (stringAPPSTR,intIndexwhereT:New() {List<T> T = setlist<t>(appstr, index); returnT; }        Private StaticList<t> setlist<t> (stringAPPENDSTR,intIndexwhereT:New() {List<T> T =NewList<t>(); Try            {                varProperties =NewT (). GetType ().                GetProperties (); varSubnum = System.web.httpcontext.current.request[appendstr + Properties[index]. Name]. Split (',').                Length;  for(inti =0; i < Subnum; i++)                {                    varR =properties; varModel =NewT (); foreach(varPinchproperties) {                        stringPVal = system.web.httpcontext.current.request[appendstr + P.name +""]; if(!string. IsNullOrEmpty (PVal)) {PVal= PVal. Split (',') [i]; stringPptypename =P.propertytype.name; P.setvalue (model, Convert.changetype (PVal, P.propertytype),NULL);                }} t.add (model); }            }            Catch(Exception ex) {Throwex; }            returnT; }    }}

Convert form elements to entity objects or collections-asp.net C #

Related Article

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.