《1》
Model 添加了一个UserInfo类 UserInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class UserInfo
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get;set; }
}
}
控制器 HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
public ActionResult UserInfo()
{
List<UserInfo> userinfo = new List<UserInfo>()
{
new UserInfo(){ Name="小w王", Age=18, Gender="女"},
new UserInfo(){ Name = "小李", Age = 19, Gender = "男"}
};
ViewData["userinfo"] = userinfo;
return View();
}
}
}
视图:UserInfo.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<!--这里是引入UserInfo类所在的的MvcApplication1.Models名称空间-->
<%@ Import Namespace="MvcApplication1.Models" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>UserInfo</title>
</head>
<body>
<div>
<table>
<tr><th>姓名</th><th>年龄</th><th>性别</th></tr>
<% List<UserInfo> userinfo = ViewData["UserInfo"] as List<UserInfo>;
foreach(var v in userinfo)
{ %>
<tr><td><%:v.Name %> </td><td><%:v.Age %></td><td><%:v.Gender %></td></tr>
<% } %>
</table>
</div>
</body>
</html>
-------------------------------------------------------------------------------------------------下面是强类型视图数据展示--------------------------------------------------------------------------
Model 添加了一个UserInfo类 UserInfo.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1.Models
{
public class UserInfo
{
public string Name { get; set; }
public int Age { get; set; }
public string Gender { get;set; }
}
}
控制器 HomeController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
List<UserInfo> userInfo = new List<UserInfo>(){
new UserInfo(){Name="小鸭子", Age=26, Gender="女"},
new UserInfo(){Name="二狗子", Age=18, Gender="男"},
new UserInfo(){Name="小鱼儿", Age=22, Gender="女"}
};
ViewData.Model = userInfo;
return View();
}
}
}
视图 Index.aspx
注意下面的两行非常重要
<!--注意该页面继承了IEnumerable<MvcApplication1.Models.UserInfo>-->
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.UserInfo>>" %>
<!DOCTYPE html>
<html>
<head runat="server">
<meta name="viewport" content="width=device-width" />
<title>这是强类型页面</title>
</head>
<body>
<div>
<table>
<tr><th>姓名</th><th>年龄</th><th>性别</th></tr>
<!--ViewData是ViewPage<TModel>泛型类的一个ViewDataDictionary类型的属性;而同时ViewData又是ViewDataDictionary类的一个对象,而ViewDataDictionary类下面又定义了Model属性,所以ViewData可以点Model。在Home控制器的Index方法里用它(ViewData.Model)来接收了一个list对象。所以这里我们就遍历这个Model-->
<% foreach(var v in Model){%>
<tr><td><%:v.Name %></td><td><%:v.Age %></td><td><%:v.Gender %></td></tr>
<%} %>
</table>
</div>
</body>
</html>