我有一个viewmodel:
public class ManageUserViewModel
{
public Entities.User User { get; set; }
public bool IsLockedOut { get; set; }
public bool IsActivated { get; set; }
public bool IsArchived { get; set; }
}
在Entities.User用户中我有一个方法:
/// Gets the incentive programs that this user is participating in.
public IEnumerable<IncentiveProgram> GetParticipatingIncentivePrograms()
{
return Node.ParticipatingIncentivePrograms
.Where(x => x.PublishingState == PublishingState.Live
&& DateTime.UtcNow.Date >= x.DateStart && DateTime.UtcNow.Date <= x.DateEnd);
}
然后我有我的控制器:
[HttpGet]
public ActionResult UserDetails(int id)
{
var user = ZincService.GetUserForId(id);
if (user == null || user.Customer.CustomerId != CurrentCustomer.CustomerId)
return DataNotFound();
ManageUserViewModel viewModel = new ManageUserViewModel();
viewModel.User = user;
viewModel.IsLockedOut = MembershipService.IsUserLocked(user.Email);
viewModel.IsActivated = user.DateTimeActivated.HasValue && MembershipService.IsUserApproved(user.Email);
viewModel.IsArchived = user.IsArchived;
viewModel.User.GetParticipatingIncentivePrograms();
return View(viewModel);
}
如何在我的视图中输出GetParticipatingIncentivePrograms? 我的视图中没有代码。
感谢
答案 0 :(得分:0)
ManageUserViewModel{
public UserViewModel User { get; set; }
}
UserViewModel {
public List<string> ParticipatingIncentivePrograms { get; set; }
}
通常最好不要将整个域图加载到视图中,因为您可能遇到几个问题(N + 1个db调用,访问封闭代理等)。我已将您的程序键入为简单的名称列表,但您可以将其扩展为字典或查找。再次保持视图模型尽可能简单明了。
更新:
由于您无法更改viewmodel,因此您似乎必须设置该值,您目前只调用它。请尝试以下
viewModel.User.ParticipatingIncentivePrograms =
viewModel.User.GetParticipatingIncentivePrograms();
如果用户上有这样的集合,或者在视图模型上将其设置为其他内容。