您的位置:首页 > 其它

MVC中Form表单的提交

2015-11-21 16:59 381 查看
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、采用Action中的方法参数名称和Form表单中Input的name名称相同。



3、服务端中的方法采用:Request.Form["BtnSave"]获取表单值;



备注:

针对方法一,我们可以创建自定义Model Binder,利用自定义Model Binder来初始化数据

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: