从复杂对象中过滤 ASP.NET MVC JsonResult 中的 Json

发布于 2024-09-30 06:19:18 字数 547 浏览 0 评论 0原文

我试图从数据模型中的复杂对象(Json)发布,不幸的是,我不想传递所有层次结构(人->HasMany Orders/ Orders HasMany Products 等),而只想传递“第一级”(例如网格视图的人名)。

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
}

我正在寻找一种解决方案来过滤 Json 对象,并且——如果可能的话——进行延迟加载并避免 sql 开销。

有什么想法吗?

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 work the lazy loading and to avoid the sql overhead.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好 2024-10-07 06:19:18

创建一个简化的人员类,仅包含您需要的属性。然后使用Linq将Person的IList转换为简化类型的列表。

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 });
}

您可以改用匿名类型,但它不会在视图中强类型化。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文