我正在从
学习MVC中的实体框架https://www.youtube.com/watch?v=8f4P8U1a2TI
当我在foreach循环中的第3行html中放置Html.DisplayNameFor()时,它会给出以下错误:
CS0121:以下方法或之间的呼叫不明确 特性: ' System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor 和
' System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor,MvcApplication1.Models.Department>
我有双重检查的HTML标签。但是找不到问题
索引视图:
@model IEnumerable<MvcApplication1.Models.Employee>
@{
ViewBag.Title = "Index";
}
<div style="font-family">
<h2>
Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
</div>
<table border="1">
<tr>
<th> </th>
<th> </th>
<th> </th>
</tr>
@foreach (var item in Model)
{
<tr>
<td> </td>
<td> </td>
<td> @Html.DisplayNameFor(modelItem => item.Name) </td>
</tr>
}
</table>
我搜索其他帖子..找不到相关内容。 请建议
员工控制器
namespace MvcApplication1.Controllers
{
public class EmployeeController : Controller
{
private EmployeeContext1 db = new EmployeeContext1();
//
// GET: /Employee/
public ActionResult Index()
{
var employees = db.Employees.Include("Department");
return View(employees.ToList());
}
//
// GET: /Employee/Details/5
public ActionResult Details(int id = 0)
{
Employee employee = db.Employees.Single(e => e.Id == id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
//
// GET: /Employee/Create
public ActionResult Create()
{
ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name");
return View();
}
//
// POST: /Employee/Create
[HttpPost]
public ActionResult Create(Employee employee)
{
if (ModelState.IsValid)
{
db.Employees.AddObject(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
return View(employee);
}
//
// GET: /Employee/Edit/5
public ActionResult Edit(int id = 0)
{
Employee employee = db.Employees.Single(e => e.Id == id);
if (employee == null)
{
return HttpNotFound();
}
ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
return View(employee);
}
//
// POST: /Employee/Edit/5
[HttpPost]
public ActionResult Edit(Employee employee)
{
if (ModelState.IsValid)
{
db.Employees.Attach(employee);
db.ObjectStateManager.ChangeObjectState(employee, System.Data.EntityState.Modified);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.DepartmentId = new SelectList(db.Departments, "Id", "Name", employee.DepartmentId);
return View(employee);
}
//
// GET: /Employee/Delete/5
public ActionResult Delete(int id = 0)
{
Employee employee = db.Employees.Single(e => e.Id == id);
if (employee == null)
{
return HttpNotFound();
}
return View(employee);
}
//
// POST: /Employee/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
Employee employee = db.Employees.Single(e => e.Id == id);
db.Employees.DeleteObject(employee);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
模型由实体框架
自动生成答案 0 :(得分:2)
看,我已经改变了答案,这肯定会对你有用。
public ActionResult Index()
{
var employees = db.Employees.Include("Department");
return View(employees);
}
为什么在这里使用BeginForm?删除它
@foreach (var item in Model)
{
<tr> <td>
@Html.DisplayFor(modelItem => item.Name)
</td> <td>
@Html.DisplayFor(modelItem => item.City)
</td> <td>
@Html.DisplayNameFor(modelItem => item.Department.Name)
</td> <td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) | <input type="submit" value="Delete" onclick="return confirm('Are you Sure wanna Delete with ID =@item.Id');"/> </td> </tr>
}
希望,它会帮助你。