Html.DisplayNameFor()出错

时间:2014-03-24 13:30:24

标签: html asp.net-mvc asp.net-mvc-4

我正在从

学习MVC中的实体框架

https://www.youtube.com/watch?v=8f4P8U1a2TI

在索引视图中,它给出以下错误:

CS0121:以下方法或属性之间的调用不明确:'System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor和

“System.Web.Mvc.Html.DisplayNameExtensions.DisplayNameFor,MvcApplication1.Models.Department>

索引视图:

@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>

    </tr>
    @foreach (var item in Model)
    {
        <tr>

            <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);
        }
    }
}

AutoGenerated Model Class:

  public partial class Employee : EntityObject
    {
        #region Factory Method

        /// <summary>
        /// Create a new Employee object.
        /// </summary>
        /// <param name="id">Initial value of the Id property.</param>
        public static Employee CreateEmployee(global::System.Int32 id)
        {
            Employee employee = new Employee();
            employee.Id = id;
            return employee;
        }

        #endregion

        #region Primitive Properties

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=true, IsNullable=false)]
        [DataMemberAttribute()]
        public global::System.Int32 Id
        {
            get
            {
                return _Id;
            }
            set
            {
                if (_Id != value)
                {
                    OnIdChanging(value);
                    ReportPropertyChanging("Id");
                    _Id = StructuralObject.SetValidValue(value);
                    ReportPropertyChanged("Id");
                    OnIdChanged();
                }
            }
        }
        private global::System.Int32 _Id;
        partial void OnIdChanging(global::System.Int32 value);
        partial void OnIdChanged();

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Name
        {
            get
            {
                return _Name;
            }
            set
            {
                OnNameChanging(value);
                ReportPropertyChanging("Name");
                _Name = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Name");
                OnNameChanged();
            }
        }
        private global::System.String _Name;
        partial void OnNameChanging(global::System.String value);
        partial void OnNameChanged();

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String Gender
        {
            get
            {
                return _Gender;
            }
            set
            {
                OnGenderChanging(value);
                ReportPropertyChanging("Gender");
                _Gender = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("Gender");
                OnGenderChanged();
            }
        }
        private global::System.String _Gender;
        partial void OnGenderChanging(global::System.String value);
        partial void OnGenderChanged();

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public global::System.String City
        {
            get
            {
                return _City;
            }
            set
            {
                OnCityChanging(value);
                ReportPropertyChanging("City");
                _City = StructuralObject.SetValidValue(value, true);
                ReportPropertyChanged("City");
                OnCityChanged();
            }
        }
        private global::System.String _City;
        partial void OnCityChanging(global::System.String value);
        partial void OnCityChanged();

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
        [DataMemberAttribute()]
        public Nullable<global::System.Int32> DepartmentId
        {
            get
            {
                return _DepartmentId;
            }
            set
            {
                OnDepartmentIdChanging(value);
                ReportPropertyChanging("DepartmentId");
                _DepartmentId = StructuralObject.SetValidValue(value);
                ReportPropertyChanged("DepartmentId");
                OnDepartmentIdChanged();
            }
        }
        private Nullable<global::System.Int32> _DepartmentId;
        partial void OnDepartmentIdChanging(Nullable<global::System.Int32> value);
        partial void OnDepartmentIdChanged();

        #endregion


        #region Navigation Properties

        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [XmlIgnoreAttribute()]
        [SoapIgnoreAttribute()]
        [DataMemberAttribute()]
        [EdmRelationshipNavigationPropertyAttribute("ComputerFirmModel", "FK__tblEmploy__Depar__4222D4EF", "tblDepartment")]
        public Department Department
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Department>("ComputerFirmModel.FK__tblEmploy__Depar__4222D4EF", "tblDepartment").Value;
            }
            set
            {
                ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Department>("ComputerFirmModel.FK__tblEmploy__Depar__4222D4EF", "tblDepartment").Value = value;
            }
        }
        /// <summary>
        /// No Metadata Documentation available.
        /// </summary>
        [BrowsableAttribute(false)]
        [DataMemberAttribute()]
        public EntityReference<Department> DepartmentReference
        {
            get
            {
                return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<Department>("ComputerFirmModel.FK__tblEmploy__Depar__4222D4EF", "tblDepartment");
            }
            set
            {
                if ((value != null))
                {
                    ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<Department>("ComputerFirmModel.FK__tblEmploy__Depar__4222D4EF", "tblDepartment", value);
                }
            }
        }

        #endregion

    }

...请建议

1 个答案:

答案 0 :(得分:3)

该错误几乎可以告诉您问题究竟是什么。你有一个实现Html.DisplayNameFor的静态扩展类,而MVC有自己的实现相同的扩展类。您无法覆盖扩展方法,因此您需要使方法的名称唯一,或者只加载一个或另一个程序集。由于您无法加载MVC,这意味着您需要将方法名称更改为Html.MyDisplayNameFor之类的其他名称。