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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM