Asp.net Mvc post表单提交多个实体模型

时间:2022-10-18 17:44:25

上一遍说道用Tuple实现Asp.net Mvc action返回多个模型实体给view,此篇发过来,实现view表单提交多个实体模型到action。

1、view代码:

@{
Layout = null;
}
@model Tuple<Model.UserInfo, Model.UserSlave>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta name='viewport' content='width=device-width,initial-scale=1.0'>
</head>
<body>
<div >
@using (Html.BeginForm("Add", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<table>
<tr>
<td>姓名</td>
<td>
@Html.TextBoxFor(model => model.Item1.Name, new { placeholder = "姓名", maxlength = "" })
</td>
</tr>
<tr>
<td>邮箱</td>
<td>
@Html.TextBoxFor(model => model.Item2.Email, new { placeholder = "邮箱", maxlength = "" })
</td>
</tr> </table> <input type="submit" name="submit" value='提交'>
}
</div>
</body>
</html>

2、action代码:

public ActionResult Add()
{
var name = Request.Form["Item1.Name"];//姓名
var email = Request.Form["Item1.Email"];//邮箱
}

通过Request.Form["Item1.Name"];获取对应实体的传过来的参数。