MVC4之ModelBinder-模型绑定

时间:2023-03-09 03:24:46
MVC4之ModelBinder-模型绑定

最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来。

十年河东十年河西,莫欺少年穷

学无止境,精益求精

   最近在做自学MVC,遇到的问题很多,索性一点点总结下。

MVC ModelBinder是MVC模型绑定的核心,本节以简单示例讲解MVC模型绑定,涉及到基本类型绑定和复合类型绑定两种。

如下:

做过webForm项目的童鞋都知道,后端如果要接收前段HTML Input标签的值,必须采用Request.Form["Key"]的方式,MVC自诞生以来,就摈弃了这种方式,使得程序更简洁,减少了程序员的开发量及代码量

Model Binder(模型绑定器),顾名思义,可以形象的理解为将数据绑定到一个 Model 的工具。这个 Model 是 Action 方法需要用到的某个类型(既可以是方法参数的类型也可以是方法内部对象的类型),要绑定到它上面的值可以来自于多种数据源。

MVC 框架内置默认的 Model Binder 是 DefaultModelBinder 类。当 Action Invoker 没找到自定义的 Binder 时,则默认使用 DefaultModelBinder。默认情况下,DefaultModelBinder 从如下 4 种途径查找要绑定到 Model 上的值:

  1. Request.Form,HTML form 元素提供的值。
  2. RouteData.Values,通过应用程序路由提供的值。
  3. Request.QueryString,所请求 URL 的 query string 值。
  4. Request.Files,客户端上传的文件。

DefaultModelBinder 按照该顺序来查找需要的值。

  1. Request.Form["Key"]
  2. RouteData.Values["Key"]
  3. Request.QueryString["Key"]
  4. Request.Files["Key"]

下面以简单示例说明:

首先:我们在Models下创建一个Person类,如下:

    public class Person
{
public string pName { get; set; }//姓名
public string pSex { get; set; }//性别
public int pAge { get; set; }//年龄
public string pAddress { get; set; }//地址 }

其次:我们创建一个控制器,如下:

    public class PersonController : Controller
{ public ActionResult Index( )
{
return View(new Person());
} [HttpPost]
public ActionResult IndexDeatail(Person model)
{
return View(model);
} }

根据控制器,我们创建如下两个View

1、index.cshtml,用于提交

@{
ViewBag.Title = "MVC Model Binding 解读及示例";
}
@using WeiXinApi.Models
@model Person
<form id="form1" action="Person/IndexDeatail" method="post">
&nbsp;&nbsp;&nbsp;姓名:@Html.EditorFor(m=> m.pName)
<br />
&nbsp;&nbsp;&nbsp;性别:@Html.EditorFor(m=> m.pSex)
<br />
&nbsp;&nbsp;&nbsp;年龄:@Html.EditorFor(m=> m.pAge)
<br />
&nbsp;&nbsp;&nbsp;地址:@Html.EditorFor(m=> m.pAddress)
<br /> <div style="height:15px; clear:both;"></div>
&nbsp;&nbsp;&nbsp;<input id="Submit1" type="submit" value="submit" />
</form>

2、IndexDeatail.cshtml,用于展示提交的数据

@{
ViewBag.Title = "MVC Model Binding 解读及示例";
}
@using WeiXinApi.Models
@model Person
<div style="height:15px; clear:both;"></div>
&nbsp;&nbsp;&nbsp;<span>@Model.pName</span><br />
&nbsp;&nbsp;&nbsp;<span>@Model.pSex</span><br />
&nbsp;&nbsp;&nbsp;<span>@Model.pAge</span><br />
&nbsp;&nbsp;&nbsp;<span>@Model.pAddress</span><br />

运行结果如下:

MVC4之ModelBinder-模型绑定

MVC4之ModelBinder-模型绑定

现在我们将Person作如下修改,改为复合类型:

    public class Person
{
public string pName { get; set; }//姓名
public string pSex { get; set; }//性别
public int pAge { get; set; }//年龄
public string pAddress { get; set; }//地址
public card cardInfo { get; set; }//银行卡信息
} public class card
{
public string BankName { get; set; }//所属银行
public string CardNum { get; set; }//账号
}

控制器方法不变,View作如下变化:

MVC4之ModelBinder-模型绑定

MVC4之ModelBinder-模型绑定

运行结果如下:

MVC4之ModelBinder-模型绑定

MVC4之ModelBinder-模型绑定

当然,我们如果不采用modelBinder,我们也可采用如下【表单收集】方法进行接收数据:

        public ActionResult Index( )
{
return View(new Person());
} [HttpPost]
public ActionResult IndexDeatail(FormCollection FormCollection)
{
Person model = new Person();
card cardModel = new card();
foreach (var key in FormCollection.AllKeys)
{
var Value = FormCollection[key].ToString();
switch (key)
{
case "pName": model.pName = Value; break;
case "pSex": model.pSex = Value; break;
case "pAge": model.pAge = Convert.ToInt32(Value); break;
case "pAddress": model.pAddress = Value; break;
case "cardInfo.BankName": cardModel.BankName = Value; break;
case "cardInfo.CardNum": cardModel.CardNum = Value; break;
}
}
model.cardInfo = cardModel;
return View(model);
}

当然,用此方法就等于回到了webForm了,不建议使用

除此之外,我们也可以采用webFrom的方法就行收集数据

        public ActionResult Index( )
{
return View(new Person());
} [HttpPost]
public ActionResult IndexDeatail()
{
Person model = new Person();
card cardModel = new card();
model.pName = Request.Form["pName"];
model.pSex = Request.Form["pSex"];
model.pAge = Convert.ToInt32(Request.Form["pAge"]);
model.pAddress = Request.Form["pAddress"];
cardModel.BankName = Request.Form["ardInfo.BankName"];
cardModel.CardNum = Request.Form["ardInfo.CardNum"];
model.cardInfo = cardModel;
return View(model);
}

如果想更深层次了解MVC ModelBinder 建议参考老A的博客:http://www.cnblogs.com/artech/archive/2012/05/21/2511086.html

谢谢

@陈卧龙的博客