Я проверил большинство из нескольких вопросов, но, к сожалению, из-за отсутствия достаточного опыта, не смогу решить эту проблему без вашей помощи.
Я не могу понять, почему происходит эта ошибка. Я думаю, что код не так.
Чтобы увидеть весь код в github
Это
UserController
using BookRental.Models;
using BookRental.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
namespace BookRental.Controllers
{
public class UserController : Controller
{
private ApplicationDbContext db;
public UserController()
{
db = ApplicationDbContext.Create();
}
// GET: User/Index
public ActionResult Index()
{
var userList = (from u in db.Users
join m in db.MembershipTypes on u.membershipTypeId equals m.membershipTypesIdPK
select new UserViewModel
{
Id = u.Id,
fname = u.fname,
lname = u.lname,
email = u.Email,
phone = u.phone,
bdate = u.bdate,
userMemTypeId = u.membershipTypeId,
MembershipTypes = (ICollection<MembershipTypes>)db.MembershipTypes.ToList().Where(n => n.membershipTypesIdPK.Equals(u.membershipTypeId)),
disabled = u.disabled
}).ToList();
return View(userList);
}
UserViewModel
using BookRental.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace BookRental.ViewModel
{
public class UserViewModel
{
[Required]
public string Id { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
public string email { get; set; }
[DataType(DataType.Password)]
public string password { get; set; }
[DataType(DataType.Password)]
public string confirmPassword { get; set; }
public ICollection<MembershipTypes> MembershipTypes { get; set; }
[Required]
public int userMemTypeId { get; set; }
[Required]
public string fname { get; set; }
[Required]
public string lname { get; set; }
[Required]
public string phone { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM dd yyyy}")]
public DateTime bdate { get; set; }
public bool disabled { get; set; }
}
}
Индексный просмотр
@model IEnumerable<BookRental.ViewModel.UserViewModel>
@using BookRental.Models
@{
ViewBag.Title = "Index";
}
<h2>Genre</h2>
@Html.Partial("_CreateButtonPartial")
<br />
<br />
<br />
<table class="table table-hover">
<thead class="thead-dark">
<tr class="row">
<th class="col">
@Html.DisplayNameFor(m => m.fname)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.lname)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.email)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.bdate)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.phone)
</th>
<th class="col">
@Html.DisplayNameFor(m => m.disabled)
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr class="row">
<td class="col">
@Html.DisplayNameFor(item => item.fname)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.lname)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.email)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.bdate)
</td>
<td class="col">
@Html.DisplayNameFor(item => item.phone)
</td>
<td class="col">
@Html.CheckBoxFor(m => item.disabled, new { @class = "disabled" })
</td>
<td class="col">
@Html.Partial("_TableButtonPartial", new IndividualButtonPartial { userId = item.Id})
</td>
</tr>
}
</tbody>
</table>
Ошибка
System.NotSupportedException
HResult=0x80131515
Message=The entity or complex type 'BookRental.Models.UserViewModel' cannot be constructed in a LINQ to Entities query.
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>