天天看點

mvc 擷取參數為實體指派

控制器中,每次為實體指派都需要接收參數,重複操作很多,是以有這個思路來為實體自動指派。

建一個基礎控制器,讓其他控制器繼承,用來完成一下基礎的操作。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;

using Util;
namespace Manweb.Controllers
{
    public class BaseController : Controller
    {
        protected string GetParam(string key)
        {
            if (Request.Form[key] != null)
            {
                return Request.Form[key];
            }
            else
            {
                throw new Exception(string.Format("需要參數:{0}", key));
            }
        }

        protected T GetEntity<T>()
        {
            Type type = typeof(T);
            T info = Activator.CreateInstance<T>();
            foreach (PropertyInfo mi in type.GetProperties())
            {
                if (!mi.IsDefined(typeof(NoWriteAttribute)))
                {
                    object value = new object();
                    bool needset = true;
                    try
                    {
                        string v = GetParam(mi.Name);

                        if (typeof(System.Enum).IsAssignableFrom(mi.PropertyType))
                        {
                            value = Enum.Parse(mi.PropertyType, v);
                        }
                        else
                        {
                            value = Convert.ChangeType(v, mi.PropertyType);
                        }
                    }
                    catch (Exception e)
                    {
                        if (mi.Name.ToLower() != "id")
                        {
                            if (!e.Message.Contains("需要參數"))
                            {
                                throw e;
                            }
                        }
                        else
                        {
                            needset = false;
                        }
                    }
                    if (needset)
                    {
                        mi.SetValue(info, value);
                    }
                }
            }
            return      

控制器中使用:

return Common.ActFuncJson(() =>
            {
                Server info = base.GetEntity<Server>();
                new;
        }      

再來一個拷貝實體的方法:

/// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source">源執行個體</param>
        /// <param name="target">目标執行個體</param>
        /// <param name="removeField">不需要複制的字段</param>
        /// <returns></returns>
        public static void CopyTo<T>(T source, T target, List<string> removeField)
        {
            Type type = typeof(T);
            foreach (PropertyInfo mi in type.GetProperties())
            {
                if (removeField.Find(s => s.ToLower() == mi.Name.ToLower()) == null)
                {
                    mi.SetValue(target, mi.GetValue(source));
                }
            }
        }      
return Common.ActFuncJson(() =>
            {
                ServerImpl impl = new Dal.Customer.ServerImpl();
                Server info = impl.Get(GetParam("id"));
                if (info == null)
                {
                    throw new ExceptionCustom();
                }
                else
                {
                    Server tmp = base.GetEntity<Server>();

                    Common.CopyTo<Server>(tmp, info, new List<string>() { "id";
        }