request参数集合绑定实体实现defaultmodebinder

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;
namespace RequestParameter1.Controllers
{

    public class Search {

        public string id { get; set; }
        public string name { get; set; }
    }
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            Type Mytype = typeof(Search);
          
            string[] arr = Request.QueryString.AllKeys;
            Search sear=new Search();
            foreach(string key in arr)
            {
              Response.Write(string.Format("{0}={1}</br>",key,Request[key]));
              PropertyInfo myproperinfo = Mytype.GetProperty(key);
              if (myproperinfo != null)
              {
                  myproperinfo.SetValue(sear, Request[key]);
              }
            }

            return Json(sear,JsonRequestBehavior.AllowGet);
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }
}

 

posted @ 2016-11-10 14:04  甜菜波波  阅读(172)  评论(0编辑  收藏  举报