Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dumping an object in an ASP.NET MVC view - an object dumper view

I have an ASP.NET MVC view, which is there only for testing and diagnosis purposes. Once a while I pass a completely different object (model) to it, and render its properties to see the state of the object. However, this needs the view to be modified for each object.

How can I create an object dumper view? By object dumper view, I mean a view which doesn't need to be changed in any way, on change of its related model. For example, on passing a user object, or a product object, or anything else, the view remain untouched, while still functioning correctly.

return View(user);
return View(product);
return View(googleAnalyticsFeed);
like image 868
Saeed Neamati Avatar asked Nov 06 '11 03:11

Saeed Neamati


2 Answers

Use the ObjectInfo helper to display the type and the value of each object you pass to it. You can use it to view the value of variables and objects in your code, plus you can see data type information about the object.

@ObjectInfo.Print(Model)
like image 62
shebert Avatar answered Oct 13 '22 21:10

shebert


Use DisplayForModel() to output a generic view or use display templates for your known objects as noted here: ASP.NET MVC 3: Output specific view for concrete implementation

This way you can customize the output if you need to as well.

like image 3
Adam Tuliper Avatar answered Oct 13 '22 20:10

Adam Tuliper