码迷,mamicode.com
首页 > Web开发 > 详细

MVC中Form表单的提交

时间:2015-11-21 18:11:11      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

Form表单是数据提交的一种,在MVC中Form表单提交到服务器中地址,接受Form表单的方式有一下几种:

1、采用实体Model类型提交,Form表单中的Input标签name要和Model对应的属性保持一致,接受Form表单的服务器端就可以直接以实体的方式存储,使用方式如下:

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" />
                    <input type="submit" name="BtnSave" value="Cancel" />
                </td>
            </tr>
           </table>
        </form>

接收服务端:

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

这种方式采用的Model Binder关联一起的。

2、采用

 

MVC中Form表单的提交

标签:

原文地址:http://www.cnblogs.com/xibei666/p/4984103.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!