Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A

Can you create TempBag just like ViewBag?( MVC Interview questions)

Download MVC interview question book PDF by clicking here. If you  want to learn MVC step by step start from this youtube video by clicking ->here.

Someone during interview to test you understanding skill may ask such kind question.
Answer for this question is, we cannot create exactly but get something like that with the help of two C# feature,

  1. Extension method  - > introduced in .net 3.5

  2. Dynamic runtime - > introduced in 4.0

Step 1
Create a simple class and inherit it from DynamicObject class (present in System.Dynamicnamesplace).


Step 2
Create a Global level object which will hold TempData.


Step 3
Create constructor for initializing GlobalTempData


Step 4
Override GetDynamicMemberNames,TryGetMember,TrySetMember of DynamicObject class as follow,


Step 5
Crete TempDataExtenion class as follows.


What’s next?

We are done with it. Let’s test our code.

Hope you enjoyed reading this and learned something.  Your comments and votes are always welcome.

If you are looking for some hard core live training on MVC,WCF,Design Patterns, BI,WPF,TFS,HTML5 visit or contact
Tags: csharp interview questions, it interview questions with answers, model view controller interview question, mvc interview answers, mvc interview questions, mvc tempdata interview questions, mvc viewbag interview questions, programming interview questions

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded