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 ...
随机推荐
- new[] 到底做了什么?
#include<iostream> #include<cstdlib> using std::cout; using std::endl; using std::hex; c ...
- python中mock的使用
什么是mock? mock在翻译过来有模拟的意思.这里要介绍的mock是辅助单元测试的一个模块.它允许您用模拟对象替换您的系统的部分,并对它们已使用的方式进行断言. 在Python2.x 中 mock ...
- for循环练习--杨辉三角
package org.hanqi.zwxx; public class Yonghuisanjiao{ public static void main(String[] args) { // TOD ...
- Warning -27077: The "vuser_init" section contains web function(s) when the "Simulate a new user on each iteration" Run-Time Setting is ON.
通过LR来录制登录过程并生成脚本,设置了自动关联,并回放录制脚本,观察回放日志发现没有报error信息,说明脚本没有问题,将脚本放入Controller中设置100个用户设置运行,发现运行一段时间开始 ...
- LoadRunner对不同协议的选择
LoadRunner对不同协议的选择 大家常用的是Loadrunner测试web(Http/Html),但其实协议多种多样.在B/S结构的网站多种业务的特点需要选择不同的协议,协议如何选择呢,寻找了相 ...
- Gitlab服务器维护
一. 内容 Gitlab服务器的更新 Gitlab服务器备份与恢复 导入Git仓库 二. Gitlab服务器的更新 1. 使用SSH登陆Gitlab服务器 2. 停止后端的unicorn服务器 [ro ...
- 6-14 Inspector s Dilemma uva12118(欧拉道路)
题意:给出一个国家城市个数n 所需走过道路个数e 每条道路长t 该国家任意两个城市之间都存在唯一道路长t 要求 :找一条最短的路遍历所有所需走过的路 一开始以为是图的匹配 但是好 ...
- 基于Laravel开发博客应用系列 —— 构建博客后台管理系统
一个完整的博客应用不能没有后台管理系统.所以在本节中我们将继续完善博客应用 —— 开发后台管理系统. 1.创建路由 在上一节十分钟创建博客项目中,已经设置过了 app/Http/routes.php, ...
- git更新到仓库
记录每次更新到仓库 现在我们手上已经有了一个真实项目的 Git 仓库,并从这个仓库中取出了所有文件的工作拷贝.接下来,对这些文件作些修改,在完成了一个阶段的目标之后,提交本次更新到仓库. 请记住,工作 ...
- java 工厂模式和内部类的完美结合
package com.bikeqx.test; public class Main{ public static void apply(ServiceFactory sf){ Service s = ...