[英]C# Linq with MVC, Cannot convert lambda expression to type 'string' because it is not a delegate type
So I am really new to MVCs.所以我对 MVC 真的很陌生。 I've been teaching myself for a project at work.我一直在为工作中的一个项目自学。
I am trying to print out my lists to my webpage from my SQL database for some inventory items and I keep getting this error:我正在尝试将我的列表从我的 SQL 数据库中的某些库存项目打印到我的网页上,但我一直收到此错误:
"Cannot convert lambda expression to type 'string' because it is not a delegate type" “无法将 lambda 表达式转换为类型‘字符串’,因为它不是委托类型”
I know there are some forms indicating adding some using statements, I still have the issue even after trying some of the solutions.我知道有一些 forms 表明添加了一些 using 语句,即使在尝试了一些解决方案后我仍然遇到问题。
My controller looks like this:我的 controller 看起来像这样:
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());
}
}
}
And my view looks like this:我的观点是这样的:
@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>
And the error is underlined at: @Html.DisplayFor(modelItem => item.itemnum)错误在以下位置加下划线:@Html.DisplayFor(modelItem => item.itemnum)
Does anyone have any ideas on how to fix this?有没有人对如何解决这个问题有任何想法?
Thank you in advance!先感谢您!
try changing @Html.DisplayName(model => model.itemnum)
to @Html.DisplayNameFor(model => model[0].itemnum)
.尝试将@Html.DisplayName(model => model.itemnum)
更改为@Html.DisplayNameFor(model => model[0].itemnum)
。 you need DisplayNameFor
to use an expression, DisplayName
just displays a string.您需要DisplayNameFor
才能使用表达式, DisplayName
只显示一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.