Http Request 到Input Model的绑定按照model的类型可分为四种情况。

  • Primitive type
  • Collection of primitive type
  • Complex type
  • Collection of complex type

首先理解Value Privider and Precedence

Model Binder是利用Value Provider来获取相关数据的。

1. Primitive type

Controller Method:

public class BindingController : Controller
{
public ActionResult Repeat(String text, Int32 number)
{
var model = new RepeatViewModel {Number = number, Text = text};
return View(model);
}
}

Http Request : 采用Query String 方式

http://server/binding/repeat?text=Dino&number=2

2.  Complex type

Complex Type:

public class RepeatText
{
public String Text { get; set; }
public Int32 Number { get; set; }
}

Controller Method:

public class ComplexController : Controller
{
public ActionResult Repeat(RepeatText inputModel)
{
var model = new RepeatViewModel
{
Title = "Repeating text",
Text = inputModel.Text,
Number = inputModel.Number
};
return View(model);
}
}

Http Request : 采用Query String 方式

http://server/binding/repeat?text=Dino&number=2

Model Binder 将从posted data中寻找Key name与Property Name匹配的。从而可以实例化RepeatText对象。

3. Collection of primitive type 

Form :

@using (Html.BeginForm())
{
<h2>List your email address(es)</h2>
foreach(var email in Model.Emails)
{
<input type="text" name="emails" value="@email" />
<br />
}
<input type="submit" value="Send" />
}

Controller Method:

public ActionResult Emails(IList<String> emails)
{
...
}

In the end, to ensure that a collection of values is passed to a controller method, you need to
ensure that elements with the same ID are emitted to the response stream.

4. Collection of complex type

Complex Type:

public class Country
{
public Country()
{
Details = new CountryInfo();
}
public String Name { get; set; }
public CountryInfo Details { get; set; }
}
public class CountryInfo
{
public String Capital { get; set; }
public String Continent { get; set; }
}

Controller Method:

[ActionName("Countries")]
[HttpPost]
public ActionResult ListCountriesForPost(IList<Country> countries)
{
...
}

Form:

@using (Html.BeginForm())
{
<h2>Select your favorite countries</h2>
var index = 0;
foreach (var country in Model.CountryList)
{
<fieldset>
<div>
<b>Name</b><br />
<input type="text"
name="countries[@index].Name"
value="@country.Name" /><br />
<b>Capital</b><br />
<input type="text"
name="countries[@index].Details.Capital"
value="@country.Details.Capital" /><br />
<b>Continent</b><br />
@{
var id = String.Format("countries[{0}].Details.Continent", index++);
}
@Html.TextBox(id, country.Details.Continent)
<br />
</div>
</fieldset>
}
<input type="submit" value="Send" />
}

总结:

如果default model binder不能适应需求则需要开发custom model binder。例如用三个TextBox实现一个DateHelper。

[置顶] ASP.NET MVC - Model Binding的更多相关文章

  1. [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider

    [ASP.NET MVC] Model Binding With NameValueCollectionValueProvider 范例下载 范例程序代码:点此下载 问题情景 一般Web网站,都是以H ...

  2. ASP.NET MVC Model验证(五)

    ASP.NET MVC Model验证(五) 前言 上篇主要讲解ModelValidatorProvider 和ModelValidator两种类型的自定义实现, 然而在MVC框架中还给我们提供了其它 ...

  3. ASP.NET MVC Model验证(四)

    ASP.NET MVC Model验证(四) 前言 本篇主要讲解ModelValidatorProvider 和ModelValidator两种类型的自定义实现,前者是Model验证提供程序,而Mod ...

  4. ASP.NET MVC Model验证(三)

    ASP.NET MVC Model验证(三) 前言 上篇中说到在MVC框架中默认的Model验证是在哪里验证的,还讲到DefaultModelBinder类型的内部执行的示意图,让大家可以看到默认的M ...

  5. ASP.NET MVC Model验证(二)

    ASP.NET MVC Model验证(二) 前言 上篇内容演示了一个简单的Model验证示例,然后在文中提及到Model验证在MVC框架中默认所处的位置在哪?本篇就是来解决这个问题的,并且会描述一下 ...

  6. ASP.NET MVC Model验证(一)

    ASP.NET MVC Model验证(一) 前言 前面对于Model绑定部分作了大概的介绍,从这章开始就进入Model验证部分了,这个实际上是一个系列的Model的绑定往往都是伴随着验证的.也会在后 ...

  7. ASP.NET MVC Model绑定(六)

    ASP.NET MVC Model绑定(六) 前言 前面的篇幅对于IValueProvider的使用做个基础的示例讲解,但是没并没有对 IValueProvider类型的实现做详细的介绍,然而MVC框 ...

  8. ASP.NET MVC Model绑定(五)

    ASP.NET MVC Model绑定(五) 前言 前面的篇幅对于IValueProvider的获取位置和所处的生成过程做了讲解,本篇将会对IValueProvider的使用做个基础的示例讲解,读完本 ...

  9. ASP.NET MVC Model绑定(四)

    ASP.NET MVC Model绑定(四) 前言 前面的篇幅对于Model绑定器IModelBinder以及实现类型.Model绑定器提供程序都作了粗略的讲解,可以把Model绑定器想象成一个大的容 ...

随机推荐

  1. ISAPI在IIS7上的配置

    主要介绍ISAPI的作用.ISAPI在IIS7上的配置.开发ISAPI的基本内容及使用VS 2008配置ISAPI DLL开发项目. 一.ISAPI介绍 缩写词=Internet Server App ...

  2. 《浅析各类DDoS攻击放大技术》

    原文链接:http://www.freebuf.com/articles/network/76021.html FreeBuf曾报道过,BT种子协议家族漏洞可用作反射分布式拒绝服务攻击(DRDoS a ...

  3. 【暑假】[基本数据结构]根据in_order与post_order构树

    Tree Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Des ...

  4. linux socket中的SO_REUSEADDR

    Welcome to the wonderful world of portability... or rather the lack of it. Before we start analyzing ...

  5. Java中可重入锁ReentrantLock原理剖析

    本文由码农网 – 吴极心原创,转载请看清文末的转载要求,欢迎参与我们的付费投稿计划! 一. 概述 本文首先介绍Lock接口.ReentrantLock的类层次结构以及锁功能模板类AbstractQue ...

  6. angular的注入实现

    angular需要对用户的传入函数进行静态分析,抽取当中的依赖,才能工作.因此用户的函数,包括控制器函数,工厂函数,服务函数与$watch回调都只是一个模板,用于取toString,真正运行的是编译后 ...

  7. 轻松学习 red5 教程 像视频一样很详细还有代码直接可Copy

    转载自:http://blog.csdn.net/hongdianking/archive/2009/11/12/4804339.aspx 最近要做一个流媒体服务器,在网上逗留了好久决定选择 red5 ...

  8. Java常用命令行工具

    命令基于Sun JDK,用于监控和诊断HotSpot的java 虚拟机. 对应的可执行文件位于$JAVA_HOME/bin/下 jps-虚拟机进程状况工具 选项 作用 -q 只输出LVMID,同进程p ...

  9. ocp 1Z0-047 61-130题解析

    61. Evaluate the following SQL statements that are issued in the given order:CREATE TABLE emp(emp_no ...

  10. URAL 2037 Richness of binary words (回文子串,找规律)

    Richness of binary words 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/B Description Fo ...