Viewdata and viewbag in Asp.net MVC

Source: Internet
Author: User

Simple comparison:

Viewdata Viewbag
It is a collection of key/value dictionaries. It is a dynamic type object.
From Asp.net MVC 1. ASP. NET mvc3
Based on ASP.net 3.5 framework Based on ASP.net 4.0 and. NET Framework
Viewdata is faster than viewbag Viewbag is slower than viewdata
The appropriate data type needs to be converted when querying data in viewpage. No type conversion is required when querying data in viewpage.
There are some types of conversionsCode Better readability
Use viewdata In the controller: Public Actionresult usingviewdata ()
{
Viewdata [ " Title " ] = " Using viewdata " ;
Viewdata [ " Projectname " ] = " My test project " ;
Viewdata [ " Projectdescription " ] = " This is test project to demo viewdata and viewbag details " ;
Viewdata [ " Startdate " ] = New Datetime ( 2011 , 1 , 1 );
Viewdata [ " Totalprice " ] = 1000 ;
Viewdata [ " Totaldays " ] = 100 ;

Dictionary < String , String > Stackholder = New Dictionary < String , String > ();
Stackholder. Add ( " Client " , " Mr. Client " );
Stackholder. Add ( " Manager " , " Mr. Joy " );
Stackholder. Add ( " Team Leader " , " Mr. Toy " );
Stackholder. Add ( " Sr. developer " , " Mr. dojoy " );
Stackholder. Add ( " Developer " , " Mr. nodoy " );
Viewdata [ " Stackholder " ] = Stackholder;

List < String > Modules = New List < String > ();
Modules. Add ( " Admin Module " );
Modules. Add ( " Shoppingcart Module " );
Modules. Add ( " CMS Module " );
Viewdata [ " Modules " ] = Modules;
Return View ();
)

Cshtml corresponding to usingviewdata View:

<H1> @ viewdata [ " Title " ] </H1>
<Div>
<Div>
<H2> Project name: @ viewdata [ " Projectname " ] </H2>
</Div>
<Div>
Projectdescription:
<P> " @ Viewdata [ " Projectdescription " ] " . </P>
</Div>
<Div>
Stack holder:
<Br/>

<Ul id = " Stakholder " >
@ Foreach ( VaR Stakerholder In Viewdata [ " Stackholder " ] As Dictionary < String , String >)
{
<Li>
@ Stakerholder. Key & nbsp;: @ stakerholder. Value
</LI>
}
</Ul>
</Div>
<Div>
Project details: <br/>
<Div>
Module list:
<Ul id = " Modulelist " >
@ Foreach ( VaR Module In Viewdata [ " Modules " ] As List < String >)
{
<Li>
@ Module
</LI>
}
</Ul>

</Div>
Project startdate: @ viewdata [ " Startdate " ] <Br/>
Project totalprice: @ viewdata [ " Totalprice " ] <Br/>
Project totadays: @ viewdata [ " Totaldays " ]
</Div>
</Div>

 

Then viewbag:

 

Public Actionresult usingviewbag ()
{
Viewbag. Title = " Using viewbag " ;
Viewbag. projectname = " My test project " ;
Viewbag. projectdescription = " This is test project to demo viewdata and viewbag details " ;
Viewbag. startdate = New Datetime ( 2011 , 1 , 1 );
Viewbag. totalprice = 1000 ;
Viewbag. totaldays = 100 ;

Dictionary < String , String > Stackholder = New Dictionary < String , String > ();
Stackholder. Add ( " Client " , " Mr. Client " );
Stackholder. Add ( " Manager " , " Mr. Joy " );
Stackholder. Add ( " Team Leader " , " Mr. Toy " );
Stackholder. Add ( " Sr. developer " , " Mr. dojoy " );
Stackholder. Add ( " Developer " , " Mr. nodoy " );
Viewbag. stackholder = stackholder;

List string modules = New List string ();
modules. add ( " admin module " );
modules. add ( " shoppingcart module " );
modules. add ( " CMS module " );
viewbag. modules = modules;
return View ();
}viewbag of cshtml corresponding to view usingviewbag:

@ viewbag. title

<Div>
<Div>
<H2> Project name: @ viewbag. projectname </H2>
</Div>
<Div>
Projectdescription:
<P>"@ Viewbag. projectdescription. </P>
</Div>
<Div>
Stack holder:
<Br/>

<Ul id = " Stakholder " >
@ Foreach ( VaR Stakerholder In Viewbag. stackholder)
{
<Li>
@ Stakerholder. Key & nbsp;: @ stakerholder. Value
</LI>
}
</Ul>
</Div>
<Div>
Project details: <br/>
<Div>
Module list:
<Ul id = " Modulelist " >
@ Foreach ( VaR Module In Viewbag. modules)
{
<Li>
@ Module
</LI>
}
</Ul>

</Div>
Project startdate: @ viewbag. startdate. tostring ( " DD-mmm-yyyy " ) <Br/>
Project totalprice: @ viewbag. totalprice? <Br/>
Project totadays: @ viewbag. totaldays
</Div>
</Div>

From: http://www.cnblogs.com/Gnepner/archive/2012/05/05/2484545.html

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.