标签:
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、采用
标签:
原文地址:http://www.cnblogs.com/xibei666/p/4984103.html