6

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);
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188

3 Answers3

26

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)
shebert
  • 319
  • 4
  • 4
  • 1
    I do a lot of mapping data to (complicated) models and this is probably the most helpful development tip I've found in years, I use it all the time, every day - it's so useful to quickly check model data on any view. Cheers. – Paul Mar 16 '17 at 19:37
  • @Paul Glad to have helped you! – shebert Mar 27 '17 at 18:08
  • `ObjectInfo.Print` works really well. Your project needs to reference `System.Web.Helpers` and `System.Web.WebPages` if it doesn't already. – MortenMoulder Dec 10 '20 at 07:52
  • Is there some way to do this in .Net 5 MVC? – Myster May 23 '22 at 00:01
3

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.

Community
  • 1
  • 1
Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
-2

As this is your testing View will suggest to use ViewBag which uses C# 4.0 dynamic feature. Before return view(); save modal into Viewbag and use viewbag to render data on View.

Edit Assuming View is just a Index view then we can easily run for each loop loop by casting ViewBag as IQuerable

swapneel
  • 3,061
  • 1
  • 25
  • 32
  • Still I need to change the view for every new model. I just want the view to dynamically loop over properties and understand nesting (complex objects), etc. So, how should I do that? – Saeed Neamati Nov 06 '11 at 03:37
  • @Saeed Neamati see my Edit or please post your View code here – swapneel Nov 06 '11 at 03:39
  • This answer does not make much sense. Using ViewBag is entirely unnecessary. Just use a dynamic Model type. Using ViewBag does not magically somehow let you use IQueryable with a View class that does not implement it. -1 – Andrew Barber Nov 06 '11 at 03:52
  • @Andrew Barber this suggestion will help to use pass data to view in a single action and asked to post view code for more details. yes there will not be any Magic. – swapneel Nov 06 '11 at 04:04
  • No, it does not help. Dynamic is better to use than ViewBag, and the point of the question is that he needs to know how to create a view that can handle the data. Adam Tuliper's answer is the correct one here. – Andrew Barber Nov 06 '11 at 04:09
  • not sure why you think answer is not going to help. give a minute and pls think to see how it is going to work to display view using Viewbag. – swapneel Nov 06 '11 at 04:13
  • 2
    ViewBag has absolutely nothing to do with displaying a class in a view; it's just one way to pass a model to the view. He has no trouble doing that. He needs to know how to *display* a model in the view without changing the view for a different model. ViewBag does exactly nothing to help there. – Andrew Barber Nov 06 '11 at 04:18