MVC中Form表单的提交
2015-11-21 16:59
381 查看
Form表单是数据提交的一种,在MVC中Form表单提交到服务器中地址,接受Form表单的方式有一下几种:
1、采用实体Model类型提交,Form表单中的Input标签name要和Model对应的属性保持一致,接受Form表单的服务器端就可以直接以实体的方式存储,使用方式如下:
Form表单:
接收服务端:
这种方式采用的Model Binder关联一起的。
2、采用Action中的方法参数名称和Form表单中Input的name名称相同。
3、服务端中的方法采用:Request.Form["BtnSave"]获取表单值;
备注:
针对方法一,我们可以创建自定义Model Binder,利用自定义Model Binder来初始化数据
1、采用实体Model类型提交,Form表单中的Input标签name要和Model对应的属性保持一致,接受Form表单的服务器端就可以直接以实体的方式存储,使用方式如下:
Form表单:
<form action="/Employee/SaveEmployee" method="post"> <table> <tr> <td>First Name:</td> <td><input type="text" id="TxtFName" name="FirstName" value="" /></td> <td>@Html.ValidationMessage("FirstName")</td> </tr> <tr> <td>Last Name:</td> <td><input type="text" id="TxtLName" name="LastName" value="" /></td> <td>@Html.ValidationMessage("LastName")</td> </tr> <tr> <td>Salary:</td> <td><input type="text" id="TxtSalary" name="Salary" value="" /></td> <td>@Html.ValidationMessage("Salary")</td> </tr> <tr> <td> <input type="submit" name="BtnSave" value="Save Employee" /> <input type="submit" name="BtnSave" value="Cancel" /> </td> </tr> </table> </form>
接收服务端:
public ActionResult SaveEmployee(Employee et, string BtnSave) { switch (BtnSave) { case "Save Employee": if (ModelState.IsValid) { EmployeeBusinessLayer empbal = new EmployeeBusinessLayer(); empbal.SaveEmployee(et); return RedirectToAction("Index"); } else { return View("CreateEmployee"); } case "Cancel": // RedirectToAction("index"); return RedirectToAction("Index"); } return new EmptyResult(); }
这种方式采用的Model Binder关联一起的。
2、采用Action中的方法参数名称和Form表单中Input的name名称相同。
3、服务端中的方法采用:Request.Form["BtnSave"]获取表单值;
备注:
针对方法一,我们可以创建自定义Model Binder,利用自定义Model Binder来初始化数据
相关文章推荐
- fcntl()功能 详解
- acess() 判断目录是否存在
- 3D Touch
- C语言:在屏幕上输出信息
- memcached&php扩展安装
- Maven 3 + Hibernate 4 + Spring 3 + Ehcache + Spring Cache
- sublime text 3中安装ctags支持函数跳转,安装convertToUtf8支持中文步骤[工具篇]
- vijos1439 区间 (排序)
- 1250: HH的米5【并查集】+【欧拉回路】
- Maven入门指南⑤:使用Nexus搭建Maven私服
- Broadcast读取短信
- js判断本地是否安装app
- Codeforces Round #332 (Div. 2)
- 详解Java编程中JavaMail API的使用
- Visual Studio 快捷键
- ORACLE迁移中的一些经验(一)批量导出sequence
- LeetCode 053 Maximum Subarray
- 算法—5.快速排序
- 线程间同步用到的几种锁机制
- openssl安装