【发布时间】:2014-02-21 13:30:16
【问题描述】:
我正在开发 asp.net MVC 4 应用程序。我有一个这样的视图模型:
public class MainViewModel
{
public List<EmailAccount> EmailAccounts { get; set; }
public List<UserContact> Contacts { get; set; }
public List<LinkedInProfile> LinkedInProfiles { get; set; }
public IConfig Config { get; set; }
}
Contacts 和 LinkedInProfiles 有多对多的关系,所以我定义了联结表:
public class LinkedInAccountConnection
{
[Key]
[Column(Order = 0)]
public Guid LinkedInAccountId { get; set; }
[Key]
[Column(Order = 1)]
public string LinkedInProfileId { get; set; }
}
鉴于我正在使用这个:
@foreach (var c in Model.Collection.Contacts.OrderByDescending(c => c.LastUpdated).Take(500))
{
@Html.Action("ContactListWidget", "Account", new { contact = c })
}
EF 代码
var user = dataRepository.GetUserByUsername(username);
Contacts = dataRepository.GetContactsAll(user.Item.UserID).Where(c => c.UserContactEmailAddresses.All(e=> !Cleansing.IsAutomatedEmailAddress(e.EmailAddress))).ToList();
foreach (var c in Contacts)
{
var userContactToLinkedInProfiles = c.UserContactToLinkedInProfiles;
foreach (var uc in userContactToLinkedInProfiles)
{
var profile = uc.LinkedInProfile;
LinkedInProfiles.Add(profile);
}
}
我想在 ContactListWidget 部分视图中显示与联系人以及相关 LinkedInProfile 相关的信息。我需要对 Viewmodel 和视图进行哪些更改?
请提出建议。
【问题讨论】:
-
你也可以把你的EF查询吗?
-
@Sampath 添加了 EF 代码。
-
我总是通过 ajax 调用加载部分视图。请参阅此处的问题和答案以获取示例stackoverflow.com/questions/21768352/…
标签: c# asp.net-mvc entity-framework asp.net-mvc-4