强类型
原始方法
controler
using strongWeb.Models;
namespace strongWeb.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
User u = new User()
{
Id = 1,
Name = "TOM",
Age = 15,
Photo = "2.jpg"
};
ViewData["data"] = u;
return View();
models
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
public string Photo { get; set; }
}
views
@using strongWeb.Models
@{
ViewBag.Title = "Home Page";
User u = ViewData["data"] as User;
}
<head>
<title>强类型</title>
</head>
<body>
<p>编号:@u.Id</p>
<p>编号:@u.Name</p>
<p>编号:@u.Age</p>
<p>编号:@u.Photo</p>
</body>
截图
第二种
controller
using strongWeb.Models;
namespace strongWeb.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
User u = new User()
{
Id = 1,
Name = "TOM",
Age = 15,
Photo = "2.jpg"
};
ViewData.Model = u;
return View();
}
}
views
@model strongWeb.Models.User
@* 强类型数据的声明,model它的类型就是user类型,model可以当做传递过来的u对象 *@
@{
//ViewBag.Title = "Home Page";
//User u = ViewData["data"] as User;
}
<head>
<title>强类型</title>
</head>
<body>
<p>编号:@Model.Id</p>
<p>编号:@Model.Name</p>
<p>编号:@Model.Age</p>
<p>编号:@Model.Photo</p>
</body>
截图
传一个集合
数据库
model
controler
views