MVC扩展ModelBinder,通过继承DefaultModelBinder把表单数据封装成类作为action参数
把视图省、市、街道表单数据,封装成一个类,作为action参数。如下:

action方法参数类型:
namespace MvcApplication1.Models
{
public class Customer
{
public string Address { get; set; }
}
}
在自定义ModelBinder中,接收视图表单数据,封装成Customer类。
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models; namespace MvcApplication1.Extension
{
public class CustomerBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType == typeof (Customer))
{
HttpRequestBase request = controllerContext.HttpContext.Request;
string province = request.Form.Get("Province");
string city = request.Form.Get("City");
string street = request.Form.Get("street"); return new Customer() {Address = province+city+street};
}
else
{
return base.BindModel(controllerContext, bindingContext);
} }
}
}
全局注册:
ModelBinders.Binders.Add(typeof(Customer), new CustomerBinder());
HomeController:
using System.Web.Mvc;
using MvcApplication1.Extension;
using MvcApplication1.Models; namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
} [HttpPost]
public ActionResult Index([ModelBinder(typeof(CustomerBinder))]Customer customer)
{
if (ModelState.IsValid)
{
return Content(customer.Address);
}
return View();
}
}
}
Home/Index.cshtml:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
@using (Html.BeginForm())
{
<table>
<tr>
<td>省</td>
<td><input type="text" id="Province" name="Province"/></td>
</tr>
<tr>
<td>市</td>
<td><input type="text" id="City" name="City"/></td>
</tr>
<tr>
<td>街道</td>
<td><input type="text" id="Street" name="Street"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="提交"/></td>
</tr>
</table>
}
提交后结果:

MVC扩展ModelBinder,通过继承DefaultModelBinder把表单数据封装成类作为action参数的更多相关文章
- form表单数据封装成json格式并提交给服务器
1.jsp代码,form表单: <form action="#" id="costForm"> <input type="hidde ...
- MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串
原文:MVC扩展ModelBinder使类型为DateTime的Action参数可以接收日期格式的字符串 如何让视图通过某种途径,把符合日期格式的字符串放到路由中,再传递给类型为DateTime的控制 ...
- MVC遇上bootstrap后的ajax表单模型验证
MVC遇上bootstrap后的ajax表单验证 使用bootstrap后他由他自带的样式has-error,想要使用它就会比较麻烦,往常使用jqueyr.validate的话只有使用他自己的样式了, ...
- JavaWeb -- Struts1 使用示例: 表单校验 防表单重复提交 表单数据封装到实体
1. struts 工作流程图 超链接 2. 入门案例 struts入门案例: 1.写一个注册页面,把请求交给 struts处理 <form action="${pageContext ...
- jquery自动将form表单封装成json的具体实现
前端页面:<span style="font-size:14px;"> <form action="" method="post&q ...
- ASP.NET From表单转实体类
原由 经常遇到 int Age=Convert.ToInt32(this.txtAge.Text); 这种蛋疼的代码,特写次方法. 之所以抛出异常是希望知道转换失败,格式错误的属性是什么,方便调试. ...
- 把表单转成json,并且name为key,value为值
http://jsfiddle.net/sxGtM/3/http://stackoverflow.com/questions/1184624/convert-form-data-to-js-objec ...
- <form> 标签 // HTML 表单 // from 表单转换成json 格式
<form> 标签 // HTML 表单 // from 表单转换成json 格式 form 表单,对开发人员来说是在熟悉不过的了,它是页面与web服务器交互时的重要信息来源 表 ...
- java后台表单验证工具类
/** * 描述 java后台表单验证工具类 * * @ClassName ValidationUtil * @Author wzf * @DATE 2018/10/27 15:21 * @VerSi ...
随机推荐
- 使用Appium 测试微信小程序和微信公众号方法
由于腾讯系QQ.微信等都是基于腾讯自研X5内核,不是google原生webview,需要打开TBS内核Inspector调试功能才能用Chrome浏览器查看页面元素,并实现Appium自动化测试微信小 ...
- 移动端布局 - REM方式
默认以宽度为640px的设计稿为基准页面,然后通过JS获取当前显示设备的尺寸,对应的调整 html 标签的font-size大小,从而实现通过以rem为单位的移动端布局适配. 具体代码 (functi ...
- 程序设计实习MOOC / 程序设计与算法(三)第一周测验
作业题: 7. 填空(2分)简单的swap 通过码是 ( 请参考公告中的“关于编程作业的说明”完成编程作业(请注意,编程题都要求提交通过码,在openjudge上提交了程序并且通过以后,就可以下载到通 ...
- Web API的几种调用方式
示例是调用谷歌短网址的API. 1. HttpClient方式: public static async void DoAsyncPost() { DateTime dateBegin = DateT ...
- windows上springboot打war部署tomcat小记
web项目,需要部署到云主机里去,现在windows里试一下. springboot项目,主要流程就只是打成war包后扔到tomcat里去,但是由于是springboot项目,有一些需要注意的地方,这 ...
- JavaQuery选择器
1.基本选择器 <!DOCTYPE html> <html> <head lang="en"> <meta charset=& ...
- sublime用浏览器打开html文件
打开Preferences - 「Key Bindings - User」,添加此行: {"keys": ["ctrl+b"],"command&qu ...
- Linux系统内存管理
<linux 内存管理模型> 下面这个图将Linux内存管理基本上描述完了,但是显得有点复杂,接下来一部分一部分的解析. 内存管理系统可以分为两部分,分别是内核空间内存管理和用户空间内存管 ...
- Django-url反向解析与csrf-token设置
url反向解析 在使用Django 项目时,一个常见的需求是获得URL 的最终形式,以用于嵌入到生成的内容中(视图中和显示给用户的URL等)或者用于处理服务器端的导航(重定向等). 人们强烈希望不要硬 ...
- Opencv学习笔记5:Opencv处理彩虹图、铜色图、灰度反转图
一.概述: 人类能够观察到的光的波长范围是有限的,并且人类视觉有一个特点,只能分辨出二十几种灰度,也就是说即使采集到的灰度图像分辨率超级高,有上百个灰度级,但是很遗憾,人们只能看出二十几个,也就是说信 ...