【发布时间】:2020-02-29 00:40:11
【问题描述】:
“IEnumerable”不包含“ImageName”的定义,并且找不到接受“IEnumerable”类型的第一个参数的可访问扩展方法“ImageName”(您是否缺少 using 指令或程序集引用?
是我得到的当前错误,但是如果有人可以帮助或指出我正确的方向,我将不胜感激。 (另外我使用的是 .net core 2.1,因为我的学校计算机不支持更高版本:/)
这里是视图:
@model IEnumerable<Lab2Phase1.Models.Car>
@{
ViewData["Title"] = "Index";
}
<strong>Index</strong>
<p>
<a asp-action="Create">Create New</a>
</p>
<div class="col-md-8">
<form action="/Cars" method="post">
@Html.TextBox("search")
<input type="submit" />
</form>
</div>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Id)
</th>
<th>
@Html.DisplayNameFor(model => model.Model)
</th>
<th>
@Html.DisplayNameFor(model => model.TopSpeed)
</th>
<th>
@Html.DisplayNameFor(model => model.ImageName)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Id)
</td>
<td>
@Html.DisplayFor(modelItem => item.Model)
</td>
<td>
@Html.DisplayFor(modelItem => item.TopSpeed)
</td>
<td>
<img src="~/Content/images/@Html.DisplayFor(modelItem => modelItem.ImageName)" style="height:200px;width:200px;"/>
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id }, new { onclick = "return confirm('Are you sure to delete?')" })
</td>
</tr>
}
</tbody>
</table>
这里是控制器:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Lab2Phase1.Models;
using Microsoft.AspNetCore.Mvc;
using Lab2Phase1.CarsContext;
using System.Collections.Specialized;
using System.Drawing;
namespace Lab2Phase1.Controllers
{
public class CarsController : Controller
{
EFDataContext _dbContext = new EFDataContext();
public IActionResult Cars()
{
var data = this._dbContext.Cars.ToList();
return View(data);
}
[HttpPost]
public IActionResult Cars(string search)
{
Console.WriteLine("boot");
//search = Request.Form["search"].ToString();
var data = _dbContext.Cars.Where(c => c.Model.Contains(search));
return View(data);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create([Bind("Id,Model,TopSpeed,ImageName")]Car model)
{
ModelState.Remove("Id");
model.Model = Request.Form["Model"];
model.TopSpeed = Request.Form["TopSpeed"];
model.ImageName = Request.Form["ImageName"];
if (ModelState.IsValid)
{
_dbContext.Cars.Add(model);
_dbContext.SaveChanges();
return RedirectToAction("cars");
}
return View();
}
public IActionResult Edit(int id)
{
Car data = _dbContext.Cars.Where(p => p.Id == id).FirstOrDefault();
return View("Create", data);
}
[HttpPost]
public IActionResult Edit(Car model)
{
ModelState.Remove("Id");
model.Id = Int32.Parse(Request.Form["Id"]);
model.Model = Request.Form["Model"];
model.TopSpeed = Request.Form["TopSpeed"];
model.ImageName = Request.Form["ImageName"];
if (ModelState.IsValid)
{
_dbContext.Cars.Update(model);
_dbContext.SaveChanges();
return RedirectToAction("cars");
}
return View("Create", model);
}
public IActionResult Delete(int id)
{
Car data = _dbContext.Cars.Where(p => p.Id == id).FirstOrDefault();
if (data != null)
{
_dbContext.Cars.Remove(data);
_dbContext.SaveChanges();
}
return RedirectToAction("cars");
}
}
}
这是模型:
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace Lab2Phase1.Models
{
public class Car
{
[Key]
public int Id { get; set; }
public string Model { get; set; }
public string TopSpeed { get; set; }
public string ImageName { get; set; }
}
}
【问题讨论】:
标签: c# .net entity-framework asp.net-core razor