我有一个视图模型,其中包含用户详细信息以及来自名为 User 和 UserDetails 的两个不同模型的扩展用户详细信息。
在我的 UsersController 中,我的 Details 方法有以下内容 -
public ViewResult Details(RegisterViewModel viewModel)
{
User currentuser = context.Users
.Include("UserDetails")
.Where(i => i.UserName == viewModel.UserName)
.First();
currentuser.UserDetails = new UserDetails();
return View(currentuser);
}
在我的详细信息视图中,我从 -
@model TRS.ViewModels.RegisterViewModel
然后尝试从视图模型中列出详细信息,例如
<tr>
<td>User Name</td>
<td>@Model.UserName</td>
</tr>
但是当我转到用户的详细信息页面时,我收到了这个错误 -
Server Error in '/' Application.
Sequence contains no elements
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains no elements
Source Error:
Line 37: {
Line 38:
Line 39: User currentuser = context.Users
Line 40: .Include("UserDetails")
Line 41: .Where(i => i.UserName == viewModel.UserName)
我显然做错了什么,但一直找不到。有任何想法吗?