简体   繁体   English

转储ASP.NET MVC视图中的对象 - 对象转储视图

[英]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. 我有一个ASP.NET MVC视图,它只用于测试和诊断目的。 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. 例如,在传递user对象或product对象或其他任何内容时,视图保持不变,同时仍能正常运行。

return View(user);
return View(product);
return View(googleAnalyticsFeed);

Use the ObjectInfo helper to display the type and the value of each object you pass to it. 使用ObjectInfo帮助程序显示传递给它的每个对象的类型和值。 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)

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 使用DisplayForModel()输出通用视图或使用已知对象的显示模板,如下所示: ASP.NET MVC 3:具体实现的输出特定视图

This way you can customize the output if you need to as well. 这样,您可以根据需要自定义输出。

As this is your testing View will suggest to use ViewBag which uses C# 4.0 dynamic feature. 由于这是您的测试,View将建议使用使用C#4.0动态功能的ViewBag。 Before return view(); 返回视图之前(); save modal into Viewbag and use viewbag to render data on View. 将模式保存到Viewbag中并使用viewbag在View上呈现数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM