ViewData and ViewBag

Source: Internet
Author: User

ViewData and ViewBag Use the same data source, so the data is just like the viewbag is no longer the dictionary's key-value pair structure, but dynamic type (http://www.cnblogs.com/kissdodog/ Archive/2013/01/20/2868644.html), which is dynamically parsed when the program is running. However, ViewBag only works if the Access keyword is a valid C # identifier. For example viewdata["have space"] with ViewBag is inaccessible. viewdata[] The Key/value,value is an object data type that is passed to the view layer to be converted to strongly typed data by using the as operation, but ViewBag determines the data type at run time and therefore does not have to be converted at all. The non-conversion types here are all types, such as being able to pass datetime,string, other custom types, and so on. But in terms of efficiency, viewdata[] is slightly higher.

In ActionResult:viewdata["list"] = peoplelist;

The conversion is required on the page:@foreach (People_model item in (viewdata["list"] as IEnumerable<people_model>)) {

You do not need to convert using ViewBag

ViewData and ViewBag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.