C# 我收到MVC5错误(无法在LINQ to Entities查询中构造)
每次我为索引启动网页时,我都会遇到这个错误。我搞不懂这个问题。这可能是一些简单的事情,我正在寻找。如果有人有这样的想法,我们将不胜感激。我不确定错误是来自分页还是搜索功能 这是索引ActionResult上的错误图片 扫描分配控制器C# 我收到MVC5错误(无法在LINQ to Entities查询中构造),c#,razor,asp.net-mvc-5,C#,Razor,Asp.net Mvc 5,每次我为索引启动网页时,我都会遇到这个错误。我搞不懂这个问题。这可能是一些简单的事情,我正在寻找。如果有人有这样的想法,我们将不胜感激。我不确定错误是来自分页还是搜索功能 这是索引ActionResult上的错误图片 扫描分配控制器 using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; us
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ScannerAssignmentList.Models;
using PagedList;
namespace ScannerAssignmentList.Controllers
{
public class ScannerAssignmentController : Controller
{
private ScannerAssignmentDb _db = new ScannerAssignmentDb();
// GET: ScannerAssignment
public ActionResult Index(int? page, string searchTerm = null)
{
var model =
_db.ScannerAssignment
.OrderByDescending(r => r.EmployeeName)
.Where(r => searchTerm == null || r.EmployeeName.Contains(searchTerm) || r.Model.Contains(searchTerm) || r.Department.Contains(searchTerm) || r.EmployeeNumber.ToString().Contains(searchTerm) || r.Serial.ToString().Contains(searchTerm))
.Select(r => new ScannerAssignmentModel
{
Id = r.Id,
EmployeeName = r.EmployeeName,
EmployeeNumber = r.EmployeeNumber,
Model = r.Model,
Serial = r.Serial,
Department = r.Department
});
// return View(model);
return View(model.ToPagedList(page ?? 1, 5));
}
// GET: ScannerAssignment/Details/5
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ScannerAssignmentModel scannerAssignmentModel = _db.ScannerAssignment.Find(id);
if (scannerAssignmentModel == null)
{
return HttpNotFound();
}
return View(scannerAssignmentModel);
}
// GET: ScannerAssignment/Create
public ActionResult Create()
{
return View();
}
// POST: ScannerAssignment/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,EmployeeName,EmployeeNumber,Model,Serial,Department,Comments")] ScannerAssignmentModel scannerAssignmentModel)
{
if (ModelState.IsValid)
{
_db.ScannerAssignment.Add(scannerAssignmentModel);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(scannerAssignmentModel);
}
// GET: ScannerAssignment/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ScannerAssignmentModel scannerAssignmentModel = _db.ScannerAssignment.Find(id);
if (scannerAssignmentModel == null)
{
return HttpNotFound();
}
return View(scannerAssignmentModel);
}
// POST: ScannerAssignment/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "Id,EmployeeName,EmployeeNumber,Model,Serial,Department,Comments")] ScannerAssignmentModel scannerAssignmentModel)
{
if (ModelState.IsValid)
{
_db.Entry(scannerAssignmentModel).State = EntityState.Modified;
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(scannerAssignmentModel);
}
// GET: ScannerAssignment/Delete/5
public ActionResult Delete(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
ScannerAssignmentModel scannerAssignmentModel = _db.ScannerAssignment.Find(id);
if (scannerAssignmentModel == null)
{
return HttpNotFound();
}
return View(scannerAssignmentModel);
}
// POST: ScannerAssignment/Delete/5
[HttpPost, ActionName("Delete")]
[ValidateAntiForgeryToken]
public ActionResult DeleteConfirmed(int id)
{
ScannerAssignmentModel scannerAssignmentModel = _db.ScannerAssignment.Find(id);
_db.ScannerAssignment.Remove(scannerAssignmentModel);
_db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_db.Dispose();
}
base.Dispose(disposing);
}
}
}
以下是索引ActionResult的视图
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<ScannerAssignmentList.Models.ScannerAssignmentModel>
@{
ViewBag.Title = "Index";
}
<br />
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Search Area</h1>
</div>
<div class="panel-body">
<div class="form-group">
<form method="get">
@Html.TextBox("searchTerm", null, new { @class = "form-control" })<span class="input-group-btn"></span>
<div class="panel-footer">
<button id="btnSearch"
class="btn btn-sm btn-primary">
<i class="glyphicon glyphicon-search"></i>
Search
</button>
<a href="@Url.Action("Index", "ScannerAssignment")" class="btn btn-sm btn-primary">
<span class="glyphicon glyphicon-cog" aria-hidden="true"></span>
Reset
</a>
<a href="@Url.Action("Create", "ScannerAssignment")" class="btn btn-sm btn-primary">
<span class="glyphicon glyphicon-plus-sign" aria-hidden="true"></span>
Add
</a>
</div>
</form>
</div>
</div>
</div>
<p>
@Html.ActionLink("Add New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.First().EmployeeName)
</th>
<th>
@Html.DisplayNameFor(model => model.First().EmployeeNumber)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Model)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Serial)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Department)
</th>
<th>
@Html.DisplayNameFor(model => model.First().Comments)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmployeeName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmployeeNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model)
</td>
<td>
@Html.DisplayFor(modelItem => item.Serial)
</td>
<td>
@Html.DisplayFor(modelItem => item.Department)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comments)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
</tr>
}
</table>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, searchterm = Request.QueryString["searchterm"] }),
new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true, DisplayItemSliceAndTotal = true })
ScannerAssignmentDb
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace ScannerAssignmentList.Models
{
public class ScannerAssignmentDb :DbContext
{
public ScannerAssignmentDb()
: base("name=DefaultConnection")
{
}
public DbSet<ScannerAssignmentModel> ScannerAssignment{ get; set; }
}
}
使用系统;
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Linq;
使用System.Web;
名称空间ScannerAssignmentList.Models
{
公共类ScannerAssignmentDb:DbContext
{
公共扫描指定数据库()
:base(“name=DefaultConnection”)
{
}
公共数据库集扫描分配{get;set;}
}
}
您需要发布ScannerAssignmentModel
的代码;我怀疑您缺少无参数构造函数,因此LINQ to Entities无法执行新的ScannerAssignmentModel。我添加了模型和数据层。如果您列出()查询并返回所有结果(不包括分页功能),是否会出现问题?您试图使MVC模型和EF实体具有相同的类?如果是这样的话,var model=\u db.ScannerAssignment.OrderByDescending(r=>r.EmployeeName).Where(r=>searchTerm==null | | r.EmployeeName.Contains(searchTerm).ToList()
work?该DbSet
已经ScannerAssignmentModel
,无需致电新的ScannerAssignmentModel
进行转换。请稍后回复以获得积分
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace ScannerAssignmentList.Models
{
public class ScannerAssignmentDb :DbContext
{
public ScannerAssignmentDb()
: base("name=DefaultConnection")
{
}
public DbSet<ScannerAssignmentModel> ScannerAssignment{ get; set; }
}
}