asp.net mvc - CS0121: The call is ambiguous between the following methods or properties: -


i learning entity framework in mvc from

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

when placed html.displaynamefor() in 3rd row of html in foreach loop .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>

i have double checked html tags . 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 .. 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 autogenerated entity framework

look have changed answer, work you.

public actionresult index()     {         var employees = db.employees.include("department");         return view(employees);     } 

why 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 sure wanna delete id =@item.id');"/> </td> </tr> } 

hope, helps you.


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 -