C# Linq with MVC,无法将 lambda 表达式转换为类型 'string',因为它不是委托类型
C# Linq with MVC, Cannot convert lambda expression to type 'string' because it is not a delegate type
所以我对 MVC 真的很陌生。我一直在自学工作中的一个项目。
我正在尝试从我的 SQL 数据库中打印我的列表到我的网页,但我不断收到此错误:
“无法将 lambda 表达式转换为类型 'string',因为它不是委托类型”
我知道有一些表格指示添加一些 using 语句,即使尝试了一些解决方案,我仍然遇到问题。
我的控制器是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LinqApplication.Models;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Entity;
namespace LinqApplication.Controllers
{
public class InventoryItemController : Controller
{
// GET: InventoryItem
public ActionResult Index()
{
IM_importEntities import = new IM_importEntities(); //our object to work with
List<InventoryItem> items = import.inventory_items.Where(x => x.itemnum == "480-15").Select(x => new InventoryItem{itemnum =x.itemnum}).ToList(); //x commands
items.ToList().ForEach(x => Console.WriteLine(x.itemnum)); //foreach loop for items
return View(items.ToList());
}
}
}
我的观点是这样的:
@model List<LinqApplication.Models.InventoryItem>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
@Html.DisplayName(model => model.itemnum)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.itemnum)
</td>
</tr>
}
</table>
错误在以下位置带有下划线:
@Html.DisplayFor(modelItem => item.itemnum)
有人知道如何解决这个问题吗?
提前致谢!
尝试将 @Html.DisplayName(model => model.itemnum)
更改为 @Html.DisplayNameFor(model => model[0].itemnum)
。
你需要 DisplayNameFor
才能使用表达式,DisplayName
只显示一个字符串。
所以我对 MVC 真的很陌生。我一直在自学工作中的一个项目。
我正在尝试从我的 SQL 数据库中打印我的列表到我的网页,但我不断收到此错误:
“无法将 lambda 表达式转换为类型 'string',因为它不是委托类型”
我知道有一些表格指示添加一些 using 语句,即使尝试了一些解决方案,我仍然遇到问题。
我的控制器是这样的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LinqApplication.Models;
using System.Data.SqlClient;
using System.Configuration;
using System.Data.Entity;
namespace LinqApplication.Controllers
{
public class InventoryItemController : Controller
{
// GET: InventoryItem
public ActionResult Index()
{
IM_importEntities import = new IM_importEntities(); //our object to work with
List<InventoryItem> items = import.inventory_items.Where(x => x.itemnum == "480-15").Select(x => new InventoryItem{itemnum =x.itemnum}).ToList(); //x commands
items.ToList().ForEach(x => Console.WriteLine(x.itemnum)); //foreach loop for items
return View(items.ToList());
}
}
}
我的观点是这样的:
@model List<LinqApplication.Models.InventoryItem>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<table class="table">
<tr>
<th>
@Html.DisplayName(model => model.itemnum)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.itemnum)
</td>
</tr>
}
</table>
错误在以下位置带有下划线: @Html.DisplayFor(modelItem => item.itemnum)
有人知道如何解决这个问题吗?
提前致谢!
尝试将 @Html.DisplayName(model => model.itemnum)
更改为 @Html.DisplayNameFor(model => model[0].itemnum)
。
你需要 DisplayNameFor
才能使用表达式,DisplayName
只显示一个字符串。