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

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).

1.1

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

2

Step 3
Create constructor for initializing GlobalTempData

3

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

4

Step 5
Crete TempDataExtenion class as follows.

5

What’s next?

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

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 www.sukesh-Marla.com or contact SukeshMarla@Gmail.com.
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
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments