Sunteți pe pagina 1din 10

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

Source : C# Corner (www.c-sharpcorner.com) Print ARTICLE

There are various wa s to pass !ata "rom a Contro##er to a $iew. I%m &oin& to !iscuss how Contro##ers interact with $iews an! speci'ca## cover wa s ou can pass !ata "rom a Contro##er to a $iew to ren!er a response (ac) to a c#ient. So* #et%s &et starte!. ViewBag $iew+a& is ver we## )nown wa to pass the !ata "rom Contro##er to $iew , even $iew to $iew. $iew+a& uses the ! namic "eature that was a!!e! in C# -... /e can sa $iew+a&0$iew1ata 2 1 namic wrapper aroun! the $iew1ata !ic3onar . Let%s see how it is use!. Between Controller and View

In the a(ove ima&e* ou can see how !ata 4ows "rom the 5Contro##er5 to the 5$iew5* an! how it #oo)s in the (rowser. Between View to View

1 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

In the a(ove ima&e* ou see how !ata is ini3a#i6e! on the 5$iew5 pa&e itse#" usin& %$iew+a&.Tit#e 0 5In!e75% an! then how it is &e8n& ren!ere! usin& %9$iew+a&.Tit#e%. /hat is 5Tit#e5: It is nothin& more than a )e * which has ver #imite! avai#a(i#it an! can (e use! on the same pa&e on# . So* the )e namin& is up to ou* use an name which ma)es ou happ . Loo) at one more case* where I wi## ta)e the a!vanta&e o" 5;o!e#5.

2 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

In the a(ove case* we have a 5;o!e#5 !e'ne! ( name 5<rien!5 that has three proper3es 5I!5* 5=ame5 an! 5A!!ress5. >n the 5Contro##er5* we have an o(?ect o" the 5<rien!5 c#ass name! 5"rn!5 an! then usin& the !ot (.) opera3on proper3es are assi&ne! then a@ache! to these proper3es o" the $iew+a&. Loo) at one more e7amp#e* in which a #ist o" stu!ents is passe! usin& $iew+a&.

3 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

So* in this wa we can pass the #ist o" stu!ents. I hope this is c#ear to ou. ViewData $iew+a& an! $iew1ata serves the same purpose in a##owin& !eve#opers to pass !ata "rom contro##ers to views. /hen ou put o(?ects in either one* those o(?ects (ecome accessi(#e in the view. Let%s #oo) at one e7amp#e:

4 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

In the a(ove ima&e* ever thin& is norma# instea! somethin& in "oreach #oop. $iew1ata is t pe! as a !ic3onar containin& 5o(?ects5* we nee! to cast $iew1ataA5Stu!ents5B to a ListCstrin&D or an IEnumera(#eCstrin&D in or!er to use the "oreach statement on it. Such as in:
@foreach (var std in (List<string>)ViewData["Students"]) OR @foreach (var std in ( !nu"era#$e<string>)ViewData["Students"])

=ow #oo) at one more (eaut o" ;$C* ou can put !ata into the $iew+a& an! access it "rom $iew1ata or put !ata in the $iew1ata an! access it "rom the $iew+a&* here ou have a## the "ree!om. ViewData to ViewBag

5 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

ViewBag to ViewData

So these two ($iew+a& an! $iew1ata) thin&s seem to wor) a#most e7act# the same. Then* what%s the !iEerence: The !iEerence is on# how ou access the !ata. $iew+a& is actua## ?ust a wrapper aroun! the $iew1ata o(?ect* an! its who#e purpose is to #et ou use access the !ata ! namica## instea! o" usin& ma&ic Cstrin&sD conversion* ou can rea#i6e it ( the a(ove e7amp#es. Some peop#e pre"er one st #e over the other. Fou can pic) whichever ma)es ou happ . In "act* (ecause the %re the same !ata ?ust with two

6 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

!iEerent wa s o" accessin& it* ou can use them interchan&ea(# #i)e $iew1ata to $iew+a& or $iew+a& to $iew1ata. It is not recommen!* however* that ou actua## use them interchan&ea(# * since it wi## wi## con"use others. =ow* so "ar we have #oo)e! into some $iew+a& an! $iew1ata* which is rea## ver use"u# (ut we can pass the !ata usin& a mo!e# a#so an! this wi## provi!e ou "u## inte##isense "eatures. ViewModel Gsin& $iew;o!e# we can a#so pass the !ata "rom the Contro##er to $iewH #et%s #oo) at the ima&e.

In the a(ove ima&e* we have mu#3p#e peop#e in a #ist "orm (ein& passe! as a $iew* simp#e. I wi## a!! one more thin& here* ou are not &oin& to &et inte##isence support or a stron&# t pe! view pa&e here* to &et it !o it this wa . Iust a!! a re"erence o" the mo!e# ( usin& the IEnumera(#e inter"ace an! ou are !one.

7 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

P#ease rea! this (#o& a#so: TempData Temp1ata is meant to (e a ver short-#ive! instance* an! ou shou#! on# use it !urin& the current an! the su(seJuent reJuests on# . Since Temp1ata wor)s this wa * ou nee! to )now "or sure what the ne7t reJuest wi## (e* an! re!irec3n& to another view is the on# 3me ou can &uarantee this. Fou can use Temp1ata to pass error messa&es or somethin& simi#ar. Example1: Gsin& Temp1ata #i)e a $iew1ata an! $iew+a&. public class FriendController : Controller { // // GET: /Friend/ public ActionResult Index() { View ata!"V Friend"# $ " eepa% & Gupta"' View(a)*V(Friend $ " eepa% & Gupta"' Te+p ata!"T Friend"# $ " eepa% & Gupta"' return View()' , , And on "View":

8 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

-p./sin) View ata: 0View ata!"V Friend"#-/p. -p./sin) View(a): 0View(a)*V(Friend-/p. -p./sin) Te+p ata: 0Te+p ata!"T Friend"# -/p. This is a simple example, but we are not using the real advantage of TempData, let's look at one more example. Example2: Using TempData to get data after redirect also. public class FriendController : Controller { // // GET: /Friend/ public ActionResult Index() { View ata!"V Friend"# $ " eepa% & Gupta"' View(a)*V(Friend $ " eepa% & Gupta"' Te+p ata!"T Friend"# $ " eepa% & Gupta"' return new RedirectResult(0"12Friend2Anot3er4a)e2")' , public ActionResult Anot3er4a)e() { return View()' , , And on "View": -p./sin) View ata: 0View ata!"V Friend"#-/p. -p./sin) View(a): 0View(a)*V(Friend-/p. -p./sin) Te+p ata: 0Te+p ata!"T Friend"#-/p. As in the above "FriendController", I'm redirecting the view, in other words the Index() view will be redirected to the AnotherPage() view instantly and now on another view after one redirect we won't be able to get data from ViewData or ViewBag but TempData will work here.

9 of 10

10/26/2013 10:38 PM

Various Ways to Pass Data From Controller to View in MVC

http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/various-ways-...

Please read this blog also: http://weblogs.asp.net/scottgu/archive/2007/12/06/asp-net-mvc-framework-part-3passing-viewdata-from-controllers-to-views.aspx. Than) ou "or usin& C# Corner

10 of 10

10/26/2013 10:38 PM

S-ar putea să vă placă și