MVC viewbag & viewdata

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.