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 ...
随机推荐
- javaweb笔记一
内连接(自然连接): 只有两个表相匹配的行才能在结果集中出现 外连接: 包括 (1)左外连接(左边的表不加限制) (2)右外连接(右边的表不加限制) (3)全外连接(左右两表都不加限制 一个空的构造器 ...
- linux虚拟机磁盘不够用以及进行扩容时遇到的问题
我使用的是:gparted live cd工具 系统是centOS6.2 使用gparted live cd工具进行无损分区,方法很简单,下载iso文件都在VMware对应的linux系统上设置CD ...
- 8-15 Shuffle uva12174
题意: 你正在使用的音乐播放器有一个所谓的乱序功能,即随机打乱歌曲的播放顺序.假设一共有s首歌,则一开始会给这s首歌随机排序,全部播放完毕后再重新随机排序.继续播放,依此类推.注意,当s首歌播放完毕之 ...
- customPage.class.php可添加js事件的分页类
用于ajax动态加载数据的分页类,分页事件可以动态添加,去除了a链接中的href地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 ...
- 10 Best jQuery and HTML5 WYSIWYG Plugins
https://www.sitepoint.com/10-best-html-wysiwyg-plugins/
- numpy.base_repr 方法解释
首先看官方文档: numpy.base_repr(number, base=2, padding=0) 将给定的 number 值,换算成给定的 base 进制(默认 2 进制)的值,以字符串的形式返 ...
- java 工厂模式和内部类的完美结合
package com.bikeqx.test; public class Main{ public static void apply(ServiceFactory sf){ Service s = ...
- annotation中的Autowired
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- MYSQL插入不能中文的问题的解决
这个问题是由于数据库的字符集不对的问题. 解决方法: 打开要用的数据库,输入命令 status 如果Client characterset 值为utf8,则要改为:set char set 'gbk' ...
- mysql数据库查询表中相邻数据的差值
select a.time ,a.sum - b.sum sum,a.time,b.time from ( rownum,) t order by time) a, ( rownum ,) t ORD ...