概述

  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表单的提交的更多相关文章

  1. mvc中form表单提交的几种形式

    第一种方式:submit 按钮 提交 <form action="MyDemand" method="post"> <span>关键字: ...

  2. django中form表单的提交:

    一,关于表单: 表单在百度百科的解释:   表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以及数据提交到服务器的方法. 表单域 ...

  3. asp.net.mvc 中form表单提交控制器的2种方法和控制器接收页面提交数据的4种方法

    MVC中表单form是怎样提交? 控制器Controller是怎样接收的? 1..cshtml 页面form提交 (1)普通方式的的提交

  4. .net mvc中的表单异步提交

    // // 摘要: // 将 <form> 开始标记写入响应. // // 参数: // ajaxHelper: // AJAX 帮助器. // // actionName: // 将处理 ...

  5. MVC中处理表单提交的方式(Ajax+Jquery)

    MVC中处理表单有很多种方法,这里说到第一种方式:Ajax+Jquery 先看下表单: <form class="row form-body form-horizontal m-t&q ...

  6. JavaScript 创建一个 form 表单并提交

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  7. 使用ajax方法实现form表单的提交(附源码)

    写在前面的话 在使用form表单的时候,一旦点击提交触发submit事件,一般会使得页面跳转,页面间的跳转等行为的控制权往往在后端,后端会控制页面的跳转及数据传递,但是在某些时候不希望页面跳转,或者说 ...

  8. 使用js控制表单重复提交(1加锁,2事件方式,3 EasyUI中解决表单重复提交)

    方法一. var flag = true; $(function() { $("#interested").click(function() { beInterested(); } ...

  9. form表单的提交方式

    开发中表单提交是很常见的,表单的提交方式也多种方式. 1.使用submit按钮提交表单  <input type="submit"/> <!DOCTYPE htm ...

随机推荐

  1. log4j配置日志文件log4j.appender.R.File相对路径方法

    方法一. 解决的办法自然是用相对路径代替绝对路径,其实log4j的FileAppender本身就有这样的机制,如:log4j.appender.logfile.File=${WORKDIR}/logs ...

  2. IDEA 进入到项目的系统文件路径

    选中项目,单击右键,在弹出的菜单中点击file path

  3. 用批处理启动MySQL命令行工具

    最近在看MySQL,安装好之后,每次在开始菜单去启动MySQL命令行工具的时候,都是直接用root用户连接我本地的数据库 输入密码开始工作,但是要连接服务器上的MySQL的话,就要去CMD下运行 : ...

  4. iOS学习之UINavigationController详解与使用(一)添加UIBarButtonItem

    http://blog.csdn.net/totogo2010/article/details/7681879 1.UINavigationController导航控制器如何使用 UINavigati ...

  5. Linux--网络通信命令(给其它用户发送广播消息)

    1.命令名称:write 执行权限:所有用户  功能描述:向另外一个用户发送信息,以CTRL+D作为结束 语法:write <用户名>root向luxh用户发送信息[root@localh ...

  6. 【GoLang】GoLang 中 make 与 new的区别

    make.new操作 make用于内建类型(map.slice 和channel)的内存分配.new用于各种类型的内存分配. 内建函数new本质上说跟其它语言中的同名函数功能一样:new(T)分配了零 ...

  7. 《oracle每日一练》免安装Oracle客户端使用PL/SQL

    免安装Oracle客户端使用PL/SQL Oracle客户端挺招人烦的,部署连接它的应用通常需要先安装它的客户端,安装程序要求在目标机器上写注册表,假设你没有洁癖的话,你仍可能被下面的事情绊住:当你的 ...

  8. gtk+-3.21.4 static build step in windows XP

    In recent days the weather is very hot Unable to sleep properly Under the state of daze research gtk ...

  9. poj 1102.LC-Display 解题报告

    题目链接:http://poj.org/problem?id=1102 题目意思:就是根据给出的格式 s 和 数字 n,输出数值 n 的 LCD 显示.数值 n 的每个数字要占据 s + 2 列 和 ...

  10. (EM算法)The EM Algorithm

    http://www.cnblogs.com/jerrylead/archive/2011/04/06/2006936.html http://blog.sina.com.cn/s/blog_a7da ...