MVC中Form表單的提交

概述前端

  Web頁面進行Form表單提交是數據提交的一種,在MVC中Form表單提交到服務器。服務端接受Form表單的方式有多種,若是一個Form有2個submit按鈕,那後臺如何判斷是哪一個按鈕提交的數據那?服務器

MVC中From提交技巧彙總post

一、採用實體Model類型提交spa

  Form表單中的Input標籤name要和Model對應的屬性保持一致,接受Form表單的服務端就能夠直接以實體Mode的方式存儲,這種方式採用的Model Binder關聯一塊兒的使用舉例以下。code

  Web前端Form表單:orm

         <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方法:blog

public ActionResult SaveEmployee(Employee et)
{      
    if (ModelState.IsValid)
    {
    EmployeeBusinessLayer empbal = new EmployeeBusinessLayer();
    empbal.SaveEmployee(et);
    return RedirectToAction("Index");
    }
    else
    {
    return View("CreateEmployee");
    }
}

 

二、一個Form有2個submit按鈕提交input

  有時候一個Form表單裏面含有多個submit按鈕,那麼如何這樣的數據如何提交到Control中那?此時能夠採用Action中的方法參數名稱和Form表單中Input的name名稱相同來解決此問題。舉例以下,頁面既有保存也有取消按鈕。string

  Web前段Form頁面:it

<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();
}

三、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"]);
    ...........
    ...........
}

四、MVC中Form提交補充

  針對方法一,咱們能夠建立自定義Model Binder,利用自定義Model Binder來初始化數據,自定義ModelBinder舉例以下:

相關文章
相關標籤/搜索