MVC viewbag & viewdata
2023-09-11 14:19:36 时间
弱类型:ViewData[""]
动态型:ViewBag dynamic
ViewData 是字典型的(Dictionary),ViewBag 不再是字典的键值对结构,而是dynamic(动态型),会在程序运行的时候动态解析。
ViewData为object型,而ViewBag为dynamic型。
dynamic型与object型的区别是在使用时它会自动根据数据类型进行转换,而object型则需要我们自己来强制转换。
控制器:
public ActionResult Top() { string sessionId = Request.Cookies["sessionId"].Value;//授权从Cookie中传递过来的Memcache的Key Object obj = MemcachedHelper.Get(sessionId);//根据key从Memcache中获取用户的信息 List<UsersJsonParam> userInfo = new List<UsersJsonParam>(); // 反序列化 userInfo = JsonConvert.DeserializeObject<List<UsersJsonParam>>(obj.ToString()); ViewBag.IlistUserInfo = userInfo; ViewData["IlistUserInfo"] = userInfo; return View(); }
chtml:
@foreach (var item in ViewBag.IlistUserInfo) { <label style="font-family: Candara; font-size: 12px; color: #fff; margin-top: 20px;">您好:</label> <label style="font-family: Candara; font-size: 12px; color: #fff; margin-top: 20px; ">@item.UserName</label> <label style="font-family: Candara; font-size: 12px; color: #fff; margin-top: 20px; ">,欢迎登录系统!</label> } @ViewBag.DateNow @foreach (var item in ViewData["IlistUserInfo"] as List<BC.Platform.UPMS.JsonParam.UsersJsonParam>) { <label style="font-family: Candara; font-size: 12px; color: #fff; margin-top: 20px;">您好:</label> <label style="font-family: Candara; font-size: 12px; color: #fff; margin-top: 20px; ">@item.UserName</label> <label style="font-family: Candara; font-size: 12px; color: #fff; margin-top: 20px; ">,欢迎登录系统!</label> } @ViewData["dateNow"]
相关文章
- nohup java -Dserver.port=8087 -Dspring.config.location=application-generic.yml -jar ${APP_HOME}/${APP_NAME} >/dev/null 2>&1
- 【BZOJ3874】[AHOI&JSOI2014]宅男计划(贪心,三分)
- 用L脚本语言实现"L脚本语言控制台"
- 华为数通笔记VXLAN&BGP EVPN
- CSDN日报190826:卷积神经网络CNN(干货满满),架构设计指南:MVC、MVP & MVVM模式
- 【前端学习之HTML&CSS】-- CSS第八篇 -- 视觉格式化模型之三 定位
- linux环境下运行程序常用的nohup和&的区别
- 《C++ AMP:用Visual C++加速大规模并行计算》——3.7 使用restrict(amp)标记的函数
- 孙玄&吴守星:全方位剖析边缘计算架构设计以及应用实践
- Java中的守护线程 & 非守护线程(简介)
- 命令式&函数式:把大象关进冰箱里问题
- 2022&2023华为OD机试 - 预订酒店(Python)
- 学习笔记(13):Python网络编程&并发编程-解决粘包问题-终极版本