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

本文介绍了一个使用ASP.NET MVC实现的简单示例,展示了如何通过反射将HTTP请求参数绑定到模型对象中。该过程包括获取请求参数、使用属性信息设置模型属性,并最终返回JSON响应。
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();
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值