- 使用ViewResult来传值
- controller写法
UserInfoModel UserInfoModel = new UserInfoModel();
UserInfoModel.Id = 1;
UserInfoModel.Name = "啦啦啦";
return View(UserInfoModel);
- view写法
注意!!!!@model那句最后没有分号,注意大小写
@model Maticsoft.Model.UserInfoModel @*最好写在最上面*@
<table style="border: 1px solid red">
<tbody>
<tr>
<td>
@Model.Id
</td>
</tr>
<tr>
<td>
@Model.Name
</td>
</tr>
</tbody>
</table>
2.使用ViewData来传值
- controller写法
UserInfoModel UserInfoModel = new UserInfoModel();
UserInfoModel.Id = 1;
UserInfoModel.Name = "啦啦啦";
ViewData["model"] = UserInfoModel;
- view写法
@{
var user = ViewData["model"] as Maticsoft.Model.UserInfoModel;
<table style="border: 1px solid black">
<tbody>
<tr>
<td>
@user.Id
</td>
</tr>
<tr>
<td>
@user.Name
</td>
</tr>
</tbody>
</table>
}
3.使用ViewBag来传值
- controller写法
UserInfoModel UserInfoModel = new UserInfoModel();
UserInfoModel.Id = 1;
UserInfoModel.Name = "啦啦啦";
ViewBag.model = UserInfoModel;// ·(点)不出来没关系,硬写
- view写法
@{
var m = ViewBag.model;
<table style="border: 1px solid orange">
<tbody>
<tr>
<td>
@m.Id
</td>
</tr>
<tr>
<td>
@m.Name
</td>
</tr>
</tbody>
</table>
}