Reputation: 263
I am learning Entity Framework in MVC from
https://www.youtube.com/watch?v=8f4P8U1a2TI
When i placed Html.DisplayNameFor() in 3rd row of html in foreach loop .It give following Error :
CS0121: The call is ambiguous between the following methods or properties: 'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor and
'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor,MvcApplication1.Models.Department>
I have double Checked html Tags . but coudnt find issue
Index View :
@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>
I search other posts .. could not find relevant. Please suggest
Employee Controller
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);
}
}
}
Model is autoGenerated by Entity Framework
Upvotes: 1
Views: 4502
Reputation: 2144
Look i have changed the answer, This will definitely work for you.
public ActionResult Index()
{
var employees = db.Employees.Include("Department");
return View(employees);
}
Why are you using BeginForm here. Remove it
@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 [email protected]');"/> </td> </tr>
}
Hope, It will helps you.
Upvotes: 2