Best way to filter domain objects for json output in an ASP.NET MVC application

后端 未结 6 1238
温柔的废话
温柔的废话 2020-12-05 20:59

If I\'m rendering a regular view in asp.net mvc the only domain object properties that show up in my page the ones I specifically write out. For example:

<         


        
6条回答
  •  一整个雨季
    2020-12-05 21:32

    I use anonymous types for this:

    var customer = from c in serviceLayer.GetCustomers()
                   where c.Id == id.Value
                   select new { FirstName = c.FirstName };
    

    This is not just a good idea. Rather, it's protection against the exception that you will get when calling Json() if your object graph contains a circular reference.

提交回复
热议问题