標籤:ring 傳遞 info users 類型 help view 20px image
弱類型: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"]
MVC viewbag & viewdata