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 ...
随机推荐
- springMVC源码分析--HttpMessageConverter数据转化(一)
之前的博客我们已经介绍了很多springMVC相关的模块,接下来我们介绍一下springMVC在获取参数和返回结果值方面的处理.虽然在之前的博客老田已经分别介绍了参数处理器和返回值处理器: (1)sp ...
- HTML5练习1
制作简历 主要代码: <!doctype html> <html> <head> <meta charset="utf-8"> &l ...
- Eclipse+Tomcat+Axis2+ADT开发环境配置
一.安装Eclipse和Tomcat 1.安装Eclipse: 2.解压缩安装apache-tomcat-6.0.41 3.tomcat配置环境变量(4个) TOMCAT_HOME D:\An ...
- ASP.NET WebAPI 01-Demo
WebAPI作为构建RESTful的平台出来有段时间了,加上最近也在用,所以想把自己的心得记录下来.我就以一个简单的增删查改作为开篇. 准备 实体类(Figure)的定义. public class ...
- ecshop用户中心菜单选项显示内容标签
ecshop用户中心菜单选项有了,那肯定需要给相应的菜单选项添加内容,下面我们主要来讲下调用内容的标签,你也可以先访问一下用户中心菜单选项修改. 用户中心页面的内容分布在两个模板文件中:user_cl ...
- annotation中的Autowired
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Django学习笔记--通用列表和详细信息视图
根据教程写完代码后,点击All books也一直跳转到index的页面 我打开了F12调试,看到点击没有出现book_list的代码,觉得应该是url的路径写得不对,但是跟教程代码对比了下,并没有发现 ...
- Java中面向对象的理解
按照惯例,先做一个简单的介绍,现在开始学习 Thinging in Java 4 ,一边看,一边记录,我都不想给自己设定时间安排了,毕竟很少实现过.所以就这样吧!不定期的更新,我都会放到博客中的. 所 ...
- Swift2.0语言教程之类的嵌套与可选链接
Swift2.0语言教程之类的嵌套与可选链接 Swift2.0语言类的嵌套 在一个类中可以嵌套一个或者多个类.它们的嵌套形式也是不同的,大致分为了两种:直接嵌套和多次嵌套.下面依次讲解这两种方式. S ...
- django定时任务
1.celery流程图: Celery的架构由三部分组成,消息中间件(message broker),任务执行单元(worker)和任务执行结果存储(task result store)组成 2.使用 ...