I am trying to post from a complex object from data model, a Json, unfortunately, I don’t want to pass all the hierarchy (person->HasMany Orders/ Orders HasMany Products etc.) but only the “first level” (for example person names for a grid view).
public JsonResult Search(string fMname, string fSname)
{
IList<Person> people = personRepository.FindAllMatchingName(fMname, fSname);
//Here with lazy loading querying only the “first level” for object
var data = people;
return Json(new { items = data });
//Here querying full object hierarchy and return the big Json
}
I am looking for a solution to filter the Json object and – if this is possible – to 开发者_Python百科work the lazy loading and to avoid the sql overhead.
Any ideas?
Create a simplified person class that only contains the properties you need. Then transform the IList of Person into a list of the simplified type using Linq.
public class SimplePerson
public string FirstName { get; set; }
public string LastName { get; set; }
}
public JsonResult Search(string fMname, string fSname)
{
IList<Person> people = personRepository.FindAllMatchingName(fMname, fSname);
var data = people.Select(m => new SimplePerson() { FirstName = m.FirstName, LastName = m.LastName }).ToList();
return Json(new { items = data });
}
You could use an anonymous type instead, but it wouldn't be strongly typed in the view.
精彩评论