0

So I am really new to MVCs. 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:

"Cannot convert lambda expression to type 'string' because it is not a delegate type"

I know there are some forms indicating adding some using statements, I still have the issue even after trying some of the solutions.

My controller looks like this:

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)

Does anyone have any ideas on how to fix this?

Thank you in advance!

  • Try following some of the answers here: https://stackoverflow.com/questions/2058487/entity-framework-cannot-convert-lambda-expression-to-type-string-because-it – Daniel Dantas Apr 08 '21 at 17:08
  • yes thats the one I've been using to try and solve the issue. I couldn't find anything that helped solve what I was looking for. I tried the "using" commands, I tried to change some of the statements, but it still comes back to this error. Thank you! – Masha J. Karabinovich Apr 08 '21 at 17:13

1 Answers1

0

try changing @Html.DisplayName(model => model.itemnum) to @Html.DisplayNameFor(model => model[0].itemnum). you need DisplayNameFor to use an expression, DisplayName just displays a string.

Efraim Newman
  • 927
  • 6
  • 21