formフォーム要素をエンティティオブジェクトまたは集合-ASPに変換する.NET C#
6386 ワード
概要:
WEBFROMの開発をする学友はすべてバックグラウンドがパラメータを受け取るのがとても面倒なことを知っています
MVCではフォームを直接集合体に変換できますが、LISTへのフォーム変換はサポートされていません
個々のオブジェクトの使用方法:
フォーム:
バックグラウンド:
集合オブジェクトの使用方法:
フォーム:
バックグラウンド:
ソース:
WEBFROMの開発をする学友はすべてバックグラウンドがパラメータを受け取るのがとても面倒なことを知っています
MVCではフォームを直接集合体に変換できますが、LIST
個々のオブジェクトの使用方法:
フォーム:
<input name='id' value='1' >
<input name='sex' value=' ' >
バックグラウンド:
//
DLC_category d = new DLC_category();
d.sex = Request["sex"];
d.id = Convert.ToInt32(Request["id"]);
//
var category = RequestToModel.GetSingleForm<DLC_category>();
集合オブジェクトの使用方法:
フォーム:
<input name='id' value='1' >
<input name='sex' value=' ' >
<input name='id' value='2' >
<input name='sex' value=' ' >
<input name='id' value='3' >
<input name='sex' value=' ' >
バックグラウンド:
List<DLC_category> categoryLists = RequestToModel.GetListByForm<DLC_category>();
ソース:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SyntacticSugar
{
/// <summary>
/// ** :
/// ** :2015-4-17
/// ** :-
/// ** :sunkaixuan
/// ** qq:610262374 , ,
/// </summary>
public class RequestToModel
{
/// <summary>
///
/// : name ,
/// </summary>
public static T GetSingleForm<T>() where T : new()
{
T t = SetList<T>(null, 0).Single();
return t;
}
/// <summary>
///
/// : name ,
/// <param name="appstr"> , name="form1.sex" appstr form1</param>
/// </summary>
public static T GetSingleForm<T>(string appstr) where T : new()
{
T t = SetList<T>(appstr, 0).Single();
return t;
}
/// <summary>
///
/// : name ,
/// </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>
///
/// : name ,
/// </summary>
/// <typeparam name="type"></typeparam>
/// <param name="appstr"> , name="form1.sex" appstr 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>
///
/// </summary>
/// <typeparam name="type"></typeparam>
/// <param name="appstr"> , name="form1.sex" appstr form1</param>
/// <typeparam name="index"> , , </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;
}
}
}