asp.net mvc - Error in Html.DisplayNameFor() -


i learning entity framework in mvc from

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

in index view ,it give following error :

cs0121: call ambiguous between following methods or properties: 'system.web.mvc.html.displaynameextensions.displaynamefor and

'system.web.mvc.html.displaynameextensions.displaynamefor,mvcapplication1.models.department>

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>      </tr>     @foreach (var item in model)     {         <tr>              <td>  @html.displaynamefor(modelitem => item.name)   </td>         </tr>     } </table> 

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

autogenerated model class :

  public partial class employee : entityobject     {         #region factory method          /// <summary>         /// create new employee object.         /// </summary>         /// <param name="id">initial value of 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         {                         {                 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         {                         {                 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         {                         {                 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         {                         {                 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         {                         {                 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         {                         {                 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         {                         {                 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      } 

... please suggest

the error pretty tells problem is. you've got static extension class implementing html.displaynamefor, while mvc has own extension class implementing same. can't override extension methods, either need make names of methods unique, or load 1 or other assembly. since can't not load mvc, means need change name of method else html.mydisplaynamefor.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -