MVC复杂类型的模型绑定
1,属性为引用类型(非集合,非数组)
//模型1
public class Contact
{
public string Name { get; set; }
public string PhoneNo { get; set; }
public string EmailAddress { get; set; }
public Address MyAddress { get; set; }
}
public class Address
{
public string Province { get; set; }
public string City { get; set; }
public string District { get; set; }
public string Street { get; set; }
}
//请求表单范例,name属性,具有显著的层次结构,注意子属性前缀用的是属性名MyAddress
<input name="Name" type="text" ... />
<input name="PhoneNo" type="text" ... />
<input name="EmailAddress" type="text" ... />
<input name="MyAddress.Province" type="text" ... />
<input name="MyAddress.City" type="text" ... />
<input name="MyAddress.District" type="text" ... />
<input name="MyAddress.Street" type="text"... />
public ActionResult Action(Contact foo){}
2,基于名称的简单类型数组绑定(名称必须相同)
//数据源范例
<input name="Foo" type="text" value=""/>
<input name="Foo" type="text" value="" />
<input name="Foo" type="text" value="" />
Public ActionResult ActionMethod(string[] foo){}//action的参数名“foo”,必须与表单中的name的值匹配
3,基于索引的对象数组绑定(索引必须从0开始,且连续,不连续会导致后面的绑定丢失)
//请求表单范例
<input name="[0].Name" type="text" value="" .../>
<input name="[0].PhoneNo" type="text" ... />
<input name="[0].EmailAddress" type="text" ... />
<input name="[1].Name" type="text" value="" .../>
<input name="[1].PhoneNo" type="text" ... />
<input name="[1].EmailAddress" type="text" ... />
public ActionResult Index(Contact[] contacts){}
4,集合(除数组和字典之外的所有实现IEnumerable<T>接口的类型)
//请求表单范例
<input name="[0].Name" type="text" value="" .../>
<input name="[0].PhoneNo" type="text" value="" .../>
<input name="[0].EmailAddress" type="text" value="" .../> <input name="[1].Name" type="text" value="" .../>
<input name="[1].PhoneNo" type="text" value="" .../>
<input name="[1].EmailAddress" type="text" value="" .../>
//集合范例
public ActionResult Action(IEnumerable<Contact> contacts){
foreach (var contact in contacts){}
}
5,字典(实现了接口IDictionary<TKey,TValue>的类型)
//请求表单范例,注意Key的值不能重复,且索引要连续
<input name="[0].Key" type="text" value="Foo" .../>
<input name="[0].Value.Name" type="text" value="" .../>
<input name="[0].Value.PhoneNo" type="text" value="" .../>
<input name="[0].Value.EmailAddress" type="text" value="a@z.com" .../> <input name="[1].Key" type="text" value="Bar" .../>
<input name="[1].Value.Name" type="text" value="Bar" .../>
<input name="[1].Value.PhoneNo" type="text" value="" .../>
<input name="[1].Value.EmailAddress" type="text" value="b@z.com" .../>
public ActionResult Action(IDictionary<string, Contact> contacts)
{
foreach (string key in contacts.Keys)
{
Contact contact = contacts[key];
}
}
6,混合应用,属性是数组
public class QuizAnswer
{
public int QuizId { get; set; }
public string[] QuizAnswerArray { get; set; }//答案为数组 }
<!--第一题,单选-->
<input name="[0].QuizId" type="hidden" value="" />
<input name="[0].QuizAnswerArray" type="radiobox" value="A" />
<input name="[0].QuizAnswerArray" type="radiobox" value="B" />
<input name="[0].QuizAnswerArray" type="radiobox" value="C" />
<input name="[0].QuizAnswerArray" type="radiobox" value="D" /> <!--第二题,多选-->
<input name="[1].QuizId" type="hidden" value="" />
<input name="[1].QuizAnswerArray" type="checkbox" value="A" />
<input name="[1].QuizAnswerArray" type="checkbox" value="B" />
<input name="[1].QuizAnswerArray" type="checkbox" value="C" />
<input name="[1].QuizAnswerArray" type="checkbox" value="D" />
//控制器代码
public ActionResult SubmitAnswer(QuizAnswer[] answerArray)
{
foreach (var answer in answerArray)
{
}
}
7,混合应用,属性是集合
模型:属性是集合的情况
//公司
public class Company
{
public string Name { get; set; }
public string City { get; set; }
public IList<Job> JobList{ get; set; }//这里是个集合
}
//职位
public class Job
{
public string Title{ get; set; }
public string Detail{ get; set; }
}
//注意集合的前缀为List
<input name="Name" type="text" ... />
<input name="City" type="text" ... /> <input name="JobList[0].Title" type="text" ... />
<input name="JobList[0].Detail" type="text" ... /> <input name="JobList[1].Title" type="text" ... />
<input name="JobList[1].Detail" type="text" ... /> <input name="JobList[2].Title" type="text" ... />
<input name="JobList[2].Detail" type="text" ... />
//controller
Public ActionResult ActionMethod(Company company)
{
foreach(var job in company.JobList){}
}
参考http://www.cnblogs.com/artech/archive/2012/05/30/default-model-binding-02.html
MVC复杂类型的模型绑定的更多相关文章
- ASP.NET MVC Model之二模型绑定
Asp.net mvc中的模型绑定,或许大家经常用,但是具体说他是怎么一回事,可能还是会有些陌生,那么,本文就带你理解模型绑定.为了理解模型绑定,本文会先给出其定义,然后对通过比,来得出使用模型绑定的 ...
- ASP.NET Core MVC/WebAPi 模型绑定探索
前言 相信一直关注我的园友都知道,我写的博文都没有特别枯燥理论性的东西,主要是当每开启一门新的技术之旅时,刚开始就直接去看底层实现原理,第一会感觉索然无味,第二也不明白到底为何要这样做,所以只有当你用 ...
- ASP.NET Core 中文文档 第四章 MVC(2.1)模型绑定
原文:Model Binding 作者:Rachel Appel 翻译:娄宇(Lyrics) 校对:许登洋(Seay).何镇汐 模型绑定介绍 ASP.NET Core MVC 中的模型绑定从 HTTP ...
- 白话学习MVC(六)模型绑定
一.什么是模型绑定? 模型绑定存在的意义就是为Action的参数提供值,例如:如下表单中提交了数据,那么Action(即:Index)的参数Id,Name的值就是表单中对应的name属性相同的值,而表 ...
- ASP.NET MVC学习之模型绑定(2)
3.手工调用模型绑定 很多情况下我们都是通过形参的方式接收来自http流中的数据,这看似是完美的,但是缺少了很多过程中的控制,所以我们就需要使用手工的方式进行绑定.下面我们通过一个例子来说明,首先打开 ...
- .NET MVC学习之模型绑定
ASP.NET MVC学习之模型绑定(2) 继ASP.NET MVC学习之模型绑定继续 3.手工调用模型绑定 很多情况下我们都是通过形参的方式接收来自http流中的数据,这看似是完美的,但是缺少了 ...
- ASP.NET Core MVC 模型绑定用法及原理
前言 查询了一下关于 MVC 中的模型绑定,大部分都是关于如何使用的,以及模型绑定过程中的一些用法和概念,很少有关于模型绑定的内部机制实现的文章,本文就来讲解一下在 ASP.NET Core MVC ...
- ASP.NET没有魔法——ASP.NET MVC 模型绑定
在My Blog中已经有了文章管理功能,可以发布和修改文章,但是对于文章内容来说,这里缺少最重要的排版功能,如果没有排版的博客很大程度上是无法阅读的,由于文章是通过浏览器查看的,所以文章的排版其实与网 ...
- ASP.NET没有魔法——ASP.NET MVC 模型绑定解析(下篇)
上一篇<ASP.NET没有魔法——ASP.NET MVC 模型绑定解析(上篇)>文章介绍了ASP.NET MVC模型绑定的相关组件和概念,本章将介绍Controller在执行时是如何通过这 ...
随机推荐
- composer.phar的作用和安装laravel5.5.4 和 vendor目录
composer.phar有什么作用 是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖 ...
- centos7防火墙 启动和关闭
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙.firewall:systemctl start firewalld.service #启动firewa ...
- 中小公司的Java工程师应该如何逆袭冲进BAT?
(1)80% Java工程师都有的迷茫 这篇文章,跟大家聊一聊很多很多很多人问我的一个问题:中小公司的Java工程师应该如何规划准备,才能跳槽进入BAT这类一线互联网公司? 之所以我用了三个 “很多” ...
- 【转】 Pro Android学习笔记(九三):AsyncTask(2):小例子
目录(?)[-] 继承AsyncTask UI操作接口 使用AsyncTask 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn. ...
- python开发初识函数:函数定义,返回值,参数
一,函数的定义 1,函数mylen叫做函数名 #函数名 #必须由字母下划线数字组成,不能是关键字,不能是数字开头 #函数名还是要有一定的意义能够简单说明函数的功能 2,def是关键字 (define) ...
- js获取用户实时地理位置
js获取用户实时地理位置 if(navigator.geolocation) { var id = navigator.geolocation.watchPosition(function(posit ...
- springboot成神之——RestTemplate访问Rest
本文介绍RestTemplate访问Rest demo package com.springlearn.learn; import java.util.Arrays; import org.sprin ...
- TEMP2
- GDB常用命令简介
1.启动调试程序 gdb 调试对象,例如gdb app 2.运行程序 run 或这简写为r 3.设置断点 有几种不同的方式 1)break line 2) break file:line 3) bre ...
- Java占位符替换工具类
import java.util.HashMap; import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFact ...