How to Perform crud in asp.net mvc
1) open the visual studio and sql Server
2) frist you can make the database in Sql Server in the name of ContractManagement and make the table in the name of Contract
and add the Columns in the of (ContractID,Name,Email,Phone and Gender) and save the table in the of Contracts
3) open the visual studio you can select the new project.you can see the project file and you can click the web option in the
Asp.net Web Application(.net Framework) and you can select and tick the Mvc File and the project will be created in automatically
4) you can make the Folder in the name of Database
5)right click database folder and select add option and then click new item and go to data option and click in the name
of (ADO.NET Entity Data Model) and given the name in Model as well.
6) Open the next page and choose the Model Content which is (EF Designer from database) and click next.
7) select the new connection which is given server name of the Sql Server name and paste the server name in the new connection login
in the server option and select the Sql Server Authentication and which you have the username and password of sql server
and seclect the database name in which correct database and click ok
8) clickm the yes option and then press next
9) in the next page entity framework 6.x and click the next page then choice the table option and click the finish
10) click the controller and add the new controller and open the new window and select the (Mvc 5 Controller with read/write actions)
click the add. open the new window and name the controller.
11)Add the project name in Libraries
12)Add the database name in Controller class
13) make the variable for list.in the index class in the from is
Public ActionResult Index()
{
var list=db.Contact.ToList();
return view(list);
}
and click the view and add the view class and run the Browser
14) you have add the create class in the from of
public ActionResult Create(Contact model)
{
try
{
//TODO:Add insert logic here
db.contacts.Add(model);
db.savechanges();
return RedirectToAction("Index");
}
catch
{
return view();
}
}
you have click view and add the view class and run in brower
15)you have add the edit class in the form of
public ActionResult Edit (int id)
{
Contact model=new Contact();
if(id>0)
{
model=db.contacts.find(id);
}
return View(model);
}
[HTTPPost]
Public ActionResult Edit(int id,Contact model)
{
try
{
if (model.ContactID>0)
{
db.Entry(model).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
you have click the return view and add of the add
16)you have delete the data you write have in get method
// GET: Management/Delete/5
public ActionResult Delete(int id)
{
var model = db.Contacts.FirstOrDefault(o => o.ContactID == id);
db.Contacts.Remove(model);
db.SaveChanges();
return View();
}
add create the view.