《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>
时间: 2024-11-09 07:39:41