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举例如下:

 

转载于:https://www.cnblogs.com/xibei666/p/4984103.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值