MVC普通数据展示及,强类型数据展示
来源:互联网 发布:气动打标机专用软件 编辑:程序博客网 时间:2024/05/20 05:53
《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>
0 0
- MVC普通数据展示及,强类型数据展示
- 数据展示
- jsp页面中判空及date类型数据的格式化展示
- sigma_grid表格数据展示及FusionChart图表展示示例
- Knn原理及Python实现、数据展示
- MySQL树形数据查询及展示
- JPivot入门 展示数据
- 使用Displaytag展示数据
- 树形数据层次展示
- 数据层级展示
- UITableView 数据展示
- UITableView 数据展示 cell
- 调取jSon数据--展示
- 分页数据展示
- 树形数据展示
- Ajax展示数据
- Geotool 展示shapefile数据
- Python数据分析、展示
- 微信朋友如何互动起来
- java的个人经验
- 单独为ie和chrome设置样式
- 判断CPU的大小端
- C++的个人心得
- MVC普通数据展示及,强类型数据展示
- HDU 4882 ZCC Loves Codefires(贪心)
- 关于函数strtok和strtok_r的使用要点和原理(一)
- __set_bit学习
- Android4.2显示yuv数据
- Formula One Races Ahead -- F1全速前进(二)
- 【Windows界面编程】界面编程,由此入门 -- 五大内容,绝对收获
- SQL
- Android 4.0 View的层