Search code examples
jqueryjsoncastle-monorail

Strongly type returned JSON using MonoRail


I am calling the following method in my controller using $.getJSON()

    [return: JSONReturnBinder]
    public object ProfileFields()
    {
        var userfields = _profileSvc.GetFields(282);
        var fields = from f in userfields
                     select new {f.ID, f.FieldName};

        return fields;
    }

My _profileSvc comes back with extra data that I don't need (actually I get NHibernate errors because the session is closed).

Is there a better way to do what I am doing? Should I strongly type the data I am returning or is this approach sufficient?

Thanks.


Solution

  • I would use a projection (SetProjection()) and SetResultTransformer() to strongly-type the result to a DTO.

    See: