MVC中Form表单的提交
概述
Web页面进行Form表单提交是数据提交的一种,在MVC中Form表单提交到服务器。服务端接受Form表单的方式有多种,如果一个Form有2个submit按钮,那后台如何判断是哪个按钮提交的数据那?
MVC中From提交技巧汇总
1、采用实体Model类型提交
Form表单中的Input标签name要和Model对应的属性保持一致,接受Form表单的服务端就可以直接以实体Mode的方式存储,这种方式采用的Model Binder关联一起的。使用举例如下。
Web前端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" />
</td>
</tr>
</table>
</form>
数据接收服务端Control方法:
public ActionResult SaveEmployee(Employee et)
{
if (ModelState.IsValid)
{
EmployeeBusinessLayer empbal = new EmployeeBusinessLayer();
empbal.SaveEmployee(et);
return RedirectToAction("Index");
}
else
{
return View("CreateEmployee");
}
}
2、一个Form有2个submit按钮提交
有时候一个Form表单里面含有多个submit按钮,那么如何这样的数据如何提交到Control中那?此时可以采用Action中的方法参数名称和Form表单中Input的name名称相同来解决此问题。举例如下,页面既有保存也有取消按钮。
Web前段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="BtnSaveLearder" value="Save Employee" />
<input type="submit" name="BtnSaveEmployee" value="Cancel" />
</td>
</tr>
</table>
</form>
数据接收服务端Control方法:通过区分BtnSave值,来走不同的业务逻辑
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();
}
3、Control服务端中的方法采用Request.Form的方式获取参数
通过Request.Form获取参数值和传统的非MVC接受的数据方式相同。举例如下:
public ActionResult SaveEmployee()
{
Employee ev=new Employee();
e.FirstName=Request.From["FName"];
e.LastName=Request.From["LName"];
e.Salary=int.Parse(Request.From["Salary"]);
...........
...........
}
4、MVC中Form提交补充
针对方法一,我们可以创建自定义Model Binder,利用自定义Model Binder来初始化数据,自定义ModelBinder举例如下:

MVC中Form表单的提交的更多相关文章
- mvc中form表单提交的几种形式
第一种方式:submit 按钮 提交 <form action="MyDemand" method="post"> <span>关键字: ...
- django中form表单的提交:
一,关于表单: 表单在百度百科的解释: 表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. 表单域 ...
- asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法
MVC中表单form是怎样提交? 控制器Controller是怎样接收的? 1..cshtml 页面form提交 (1)普通方式的的提交
- .net mvc中的表单异步提交
// // 摘要: // 将 <form> 开始标记写入响应. // // 参数: // ajaxHelper: // AJAX 帮助器. // // actionName: // 将处理 ...
- MVC中处理表单提交的方式(Ajax+Jquery)
MVC中处理表单有很多种方法,这里说到第一种方式:Ajax+Jquery 先看下表单: <form class="row form-body form-horizontal m-t&q ...
- JavaScript 创建一个 form 表单并提交
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 使用ajax方法实现form表单的提交(附源码)
写在前面的话 在使用form表单的时候,一旦点击提交触发submit事件,一般会使得页面跳转,页面间的跳转等行为的控制权往往在后端,后端会控制页面的跳转及数据传递,但是在某些时候不希望页面跳转,或者说 ...
- 使用js控制表单重复提交(1加锁,2事件方式,3 EasyUI中解决表单重复提交)
方法一. var flag = true; $(function() { $("#interested").click(function() { beInterested(); } ...
- form表单的提交方式
开发中表单提交是很常见的,表单的提交方式也多种方式. 1.使用submit按钮提交表单 <input type="submit"/> <!DOCTYPE htm ...
随机推荐
- 对C++虚函数、虚函数表的简单理解
一.虚函数的作用 以一个通用的图形类来了解虚函数的定义,代码如下: #include "stdafx.h" #include <iostream> using name ...
- php处理数据库数据,每处理一个数据返回客户端显示当前状态的方法。
php处理大量数据,每处理一个数据返回客户端显示当前状态的方法. 类似于dedecms生成静态页 想法: 客户端发送请求 服务器端接受请求,开始统计所需处理的数据量 将所需处理数据按一定规则排列,发送 ...
- Units Problem: How to read text size as custom attr from xml and set it to TextView in java code
Here is this topic’s background: I defined a custom View which extends FrameLayout and contains a Te ...
- Python自动化之常用模块
1 time和datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) #返回处理 ...
- mysql中like用法
like 的通配符有两种 %(百分号):代表零个.一个或者多个字符. _(下划线):代表一个数字或者字符. 1. name以"李"开头 where name like '李%' 2 ...
- 5. apktool 给XX手机卫士加广告页
一. 编写广告页 写一个广告页面,并调用其他页面的demo (1) 设计界面如下 (2) 编写代码如下 public class SplashActivity extends Activity { ...
- iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem
http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...
- list<T>与ObservableCollection<T>
list<T>与ObservableCollection<T>的用法基本上是一样的.区别: list<T>: 当T继承于INotifyPropertyChanged ...
- We will be discontinuing the Nitrous Development Platform and Cloud IDE on November 14th, 2016.
我表示我很难过 Nitrous We will be discontinuing the Nitrous Development Platform and Cloud IDE on November ...
- 【python】lxml中多个xml采用相同节点时出现的问题
今天突然发现了一个lxml的坑. 假设我们有一个节点 <id>123</id> 有两个父节点都要用上述节点,则必须把上面的节点写两遍!用同一个会出错! 出错例子: #!/usr ...