Advertisement
murad45

RifatContactsCreatecshtml.cs

Sep 24th, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Web;
  8. using System.Web.Mvc;
  9. using ContactManagement.Models;
  10.  
  11. namespace ContactManagement.Controllers
  12. {
  13.     public class Contacts : Controller
  14.     {
  15.         private RifatContactEntities db = new RifatContactEntities();
  16.  
  17.         // GET: Contacts
  18.         public ActionResult Index()
  19.         {
  20.             return View(db.Contacts.ToList());
  21.         }
  22.  
  23.         // GET: Contacts/Details/5
  24.         public ActionResult Details(int? id)
  25.         {
  26.             if (id == null)
  27.             {
  28.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  29.             }
  30.             Contact contact = db.Contacts.Find(id);
  31.             if (contact == null)
  32.             {
  33.                 return HttpNotFound();
  34.             }
  35.             return View(contact);
  36.         }
  37.  
  38.         // GET: Contacts/Create
  39.         public ActionResult Create()
  40.         {
  41.             return View();
  42.         }
  43.  
  44.         // POST: Contacts/Create
  45.         // To protect from overposting attacks, enable the specific properties you want to bind to, for
  46.         // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  47.         [HttpPost]
  48.         [ValidateAntiForgeryToken]
  49.         public ActionResult Create([Bind(Include = "ID,First_Name,Last_Name,Phone_Number,Email,Category")] Contact contact)
  50.         {
  51.             if (ModelState.IsValid)
  52.             {
  53.                 db.Contacts.Add(contact);
  54.                 db.SaveChanges();
  55.                 return RedirectToAction("Index");
  56.             }
  57.  
  58.             return View(contact);
  59.         }
  60.  
  61.         // GET: Contacts/Edit/5
  62.         public ActionResult Edit(int? id)
  63.         {
  64.             if (id == null)
  65.             {
  66.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  67.             }
  68.             Contact contact = db.Contacts.Find(id);
  69.             if (contact == null)
  70.             {
  71.                 return HttpNotFound();
  72.             }
  73.             return View(contact);
  74.         }
  75.  
  76.         // POST: Contacts/Edit/5
  77.         // To protect from overposting attacks, enable the specific properties you want to bind to, for
  78.         // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
  79.         [HttpPost]
  80.         [ValidateAntiForgeryToken]
  81.         public ActionResult Edit([Bind(Include = "ID,First_Name,Last_Name,Phone_Number,Email,Category")] Contact contact)
  82.         {
  83.             if (ModelState.IsValid)
  84.             {
  85.                 db.Entry(contact).State = EntityState.Modified;
  86.                 db.SaveChanges();
  87.                 return RedirectToAction("Index");
  88.             }
  89.             return View(contact);
  90.         }
  91.  
  92.         // GET: Contacts/Delete/5
  93.         public ActionResult Delete(int? id)
  94.         {
  95.             if (id == null)
  96.             {
  97.                 return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
  98.             }
  99.             Contact contact = db.Contacts.Find(id);
  100.             if (contact == null)
  101.             {
  102.                 return HttpNotFound();
  103.             }
  104.             return View(contact);
  105.         }
  106.  
  107.         // POST: Contacts/Delete/5
  108.         [HttpPost, ActionName("Delete")]
  109.         [ValidateAntiForgeryToken]
  110.         public ActionResult DeleteConfirmed(int id)
  111.         {
  112.             Contact contact = db.Contacts.Find(id);
  113.             db.Contacts.Remove(contact);
  114.             db.SaveChanges();
  115.             return RedirectToAction("Index");
  116.         }
  117.  
  118.         protected override void Dispose(bool disposing)
  119.         {
  120.             if (disposing)
  121.             {
  122.                 db.Dispose();
  123.             }
  124.             base.Dispose(disposing);
  125.         }
  126.     }
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement