我有一個視圖模型,其中包含用戶詳細信息以及來自兩個不同模型(稱爲User和UserDetails)的擴展用戶詳細信息。從視圖模型返回單個用戶的詳細信息
在我UsersController我已經在我的詳細方法如下 -
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
,然後嘗試列出從視圖模型例如在detials
<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)
我明明做錯事,但一直沒能找到什麼。有任何想法嗎?
謝謝,細節操作是從索引頁面上的鏈接調用的,所以它類似於http:// localhost:49363/Users/Details/jsmith,其中jsmith是用戶名,因此我認爲會有相應的爲此在數據庫中輸入。我應該只是傳遞用戶名作爲參數,然後創建視圖模型的實例? –