- ASP.NET
- 901
- March-30-2018
- by Arsal Ahmed
In this example it will show you how to autocrud asp.net for mvc in visual studio and import db in asp.net via framework
1. Create table in the database for sql server
2. In visual studio, open the new project and select web application .net framework
3. Select on the project which has been made in the web application and click on the new folder.
4. Click on the new folder and write name database.
5. Once the name database has been written and then add a new item while clicking the database folder
6. Once the new item has been click and search data to find the name ADO.NET Entity data model.
7. Once the data model has been click and select the model container which is the first one is EF designer from database.
8.Then select the following table to import database table from sql server and once the finish button has been pressed.
9. The project will show the database import and the database which it should include database server name, database name as well from the sql server to import it in the visual studio.
10. Once the database has been imported and then it will show the table in the visual studio.
11. The controller folder must be selected and must press controller to select mvc read.actions written which is in the second part.
12. Once it has been selected the name of the controller should be written in the controller name
13. Once the controller has been created, then the controller code will be shown in the project
Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrudOperation.Database;
namespace CrudOperation.Controllers
{
public class Home2Controller : Controller
{
ServisShopEntities db = new ServisShopEntities();
// GET: Home2
public ActionResult Index()
{
var list = db.TypeID.ToList();
return View(list);
}
// GET: Home2/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: Home2/Create
public ActionResult Create()
{
return View();
}
// POST: Home2/Create
[HttpPost]
public ActionResult Create(TypeID model)
{
try
{
// TODO: Add insert logic here
db.TypeID.Add(model);
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Home2/Edit/5
public ActionResult Edit(int id)
{
TypeID model = new TypeID();
if (id > 0)
{
model = db.TypeID.Find(id);
}
return View(model);
}
// POST: Home2/Edit/5
[HttpPost]
public ActionResult Edit(int id, TypeID model)
{
try
{
// TODO: Add update logic here
if (model.TypeID1 > 0)
{
db.Entry(model).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Home2/Delete/5
public ActionResult Delete(int id)
{
var model = db.TypeID.FirstOrDefault(w => w.TypeID1 == id);
db.TypeID.Remove(model);
db.SaveChanges();
return View();
}
// POST: Home2/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
// TODO: Add delete logic here
return RedirectToAction("Index");
}
catch
{
return View(id);
}
}
}
}
14. The code must written and make four pages for views are list, create, edit and delete. While making the view the user should select the rebuild solution option in order to implement the page properly.
15. The code must be written according to the example that is showned above in the eaxmple.
16. The list and create will show the pages, but for edit and delete the user show view the page in the index page to find the edit and delete
17. If the user implements the page for edit and delete in single page, then it will not the show the page in the internet.