Implementation of converting form form elements into entity objects or collections in ASP.NET

  • 2021-06-28 12:17:41
  • OfStack

Introduction:

The students who are doing WEBFROM development all know that background reception parameters are very troublesome

Forms can be converted directly to set entities in MVC, but conversion to LIST is not supported. < T > This collection

Usage of a single object:

Form:


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

Backstage:


// Previous Writing
            DLC_category d = new DLC_category();
            d.sex = Request["sex"];
            d.id = Convert.ToInt32(Request["id"]);
            // modern script
            var category = RequestToModel.GetSingleForm<DLC_category>();

Usage 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=' female ' >

Backstage:

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

Source code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace SyntacticSugar
{
  /// <summary>
  /// **  Description: Form Help Class 
  /// **  Founded: 2015-4-17
  /// **  Modification time: -
  /// **  Author: sunkaixuan
  /// ** qq : 610262374  Welcome to Exchange , Improve Together  , Where the naming grammar is not well written, you are welcome to give valuable suggestions 
  /// </summary>
  public class RequestToModel
  {
 
    /// <summary>
    ///  Submit the form to get a single image by reflection 
    ///  Note: Form controls name Must contain the first of the corresponding classes 1 Fields, otherwise an error will be reported 
    /// </summary>
    public static T GetSingleForm<T>() where T : new()
    {
      T t = SetList<T>(null, 0).Single();
      return t;
    }
 
 
    /// <summary>
    ///  Submit the form to get a single image by reflection 
    ///  Note: Form controls name Must contain the first of the corresponding classes 1 Fields, otherwise an error will be reported 
    /// <param name="appstr"> Control prefix , such as  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 the form to get multiple objects through reflection 
    ///  Note: Form controls name Must contain the first of the corresponding classes 1 Fields, otherwise an error will be reported 
    /// </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 the form to get multiple objects through reflection 
    ///  Note: Form controls name Must contain the first of the corresponding classes 1 Fields, otherwise an error will be reported 
    /// </summary>
    /// <typeparam name="type"></typeparam>
    /// <param name="appstr"> Control prefix , such as  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 the form to get multiple objects through reflection 
    /// </summary>
    /// <typeparam name="type"></typeparam>
    /// <param name="appstr"> Control prefix , such as  name="form1.sex" appstr Can be set to form1</param>
    /// <typeparam name="index"> Form Control 1 Control corresponding to the index number of the field in the class , The special case may be 2 No. 3 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;
    }
  }
}


Related articles: