Sunday, 15 February 2015

c# - CRUD operations in MVC 3 without using db context -



c# - CRUD operations in MVC 3 without using db context -

i need help .actually using ado net 4.0 .net framework , create own database using app_data in solution explorer .mdf extension.

using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using crudoperations.models; using system.data.entity; namespace crudoperations.controllers { public class employeecontroller : controller { // // get: /employee/ employentities employcontext; public actionresult index() { using (var details= new employentities()) { homecoming view(details.employs.tolist()); } } // // get: /employee/details/5 public actionresult details(int id) { employcontext = new employentities(); employ _tempemploy = new employ(); _tempemploy.id = id; homecoming view(_tempemploy); } // // get: /employee/create public actionresult create() { homecoming view(); } // // post: /employee/create [httppost] public actionresult create(employ objemploy) { seek { employcontext = new employentities(); employcontext.addtoemploys(objemploy); employcontext.savechanges(); homecoming redirecttoaction("index"); } grab { homecoming view(); } } public actionresult edit(int id) { employcontext = new employentities(); employ _employ = new employ(); //_employ = ( employ // id = id) homecoming view(); } [httppost] public actionresult edit(int id, employ objemploy) { seek { homecoming redirecttoaction("index"); } grab { homecoming view(); } } // // get: /employee/delete/5 public actionresult delete(int id) { employ _tempemploy = new employ(); homecoming view(_tempemploy); } // // post: /employee/delete/5 [httppost] public actionresult delete(int id, employ objemploy) { seek { //employcontext = new employentities(); //employ _tempemploy = new employ(); //_tempemploy.id = id; //employ _employ = employcontext.find(_tempemploy); employcontext.deleteobject(objemploy); employcontext.savechanges(); homecoming redirecttoaction("index"); } grab { homecoming redirecttoaction("delete", new { id = id, savechangeserror = true }); } } public iview detailsmodel { get; set; } }

}

actually i'm getting problem in accessing info using id i.e in db context there method "find(id)" there no such type of method in object context.what have accessing info edit ,details , delete info database

you can utilize firstordefault() fetching id, fetch first matching object.

db.users.firstordefault(x => x.userid== id);

hope, may help you. have anice day.

c# asp.net-mvc

No comments:

Post a Comment