Test Equipment Database Source Code – Equipment Controller -C#

Sample Source Code – Equipment Assets Controller

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using EDB.Models;
using EDBApplication.Models;
using System.Data.Entity.Infrastructure;

namespace EDB.Controllers
{
    public class EquipmentAssetsController : Controller
    {
        private EquipmentAssetsContext db = new EquipmentAssetsContext();

        // GET: EquipmentAssets
        public ActionResult Index()
        {
            var equipmentAssets = db.EquipmentAssets.Include(e => e.EquipmentGroup).Include(e => e.EquipmentManufacturer).Include(e => e.EquipmentStatu).Include(e => e.GeoLocation).Include(e => e.InternalLocation);
            //var equipmentAssets = db.EquipmentAssets.Include(e => e.EquipmentGroup);
            int numberOfrecords = 15; // read from user
            //equipmentAssets.OrderByDescending(x => x.AssetLoggedDate).Take(numberOfrecords);
            equipmentAssets = equipmentAssets.OrderByDescending(x => x.AssetLoggedDate).Take(30);

            return View(equipmentAssets.ToList());
        }

        // GET: EquipmentAssets/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EquipmentAsset equipmentAsset = db.EquipmentAssets.Find(id);
            if (equipmentAsset == null)
            {
                return HttpNotFound();
            }
            return View(equipmentAsset);
        }

        // GET: EquipmentAssets/Create
        public ActionResult Create()
        {
            ViewBag.EquipmentGroupID = new SelectList(db.EquipmentGroups, "EquipmentGroupID", "EquipmentGroupName");
            ViewBag.EquipmentManufacturerID = new SelectList(db.EquipmentManufacturers, "EquipmentManufacturerID", "EquipmentManufacturerName");
            ViewBag.EquipmentStatusID = new SelectList(db.EquipmentStatus, "EquipmentStatusID", "EquipmentStatusDescription");
            ViewBag.GeoLocationID = new SelectList(db.GeoLocations, "GeoLocationID", "GeoLocationName");
            ViewBag.InternalLocationID = new SelectList(db.InternalLocations, "InternalLocationID", "InternalLocationName");
            return View();
        }

        // POST: EquipmentAssets/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "EquipmentAssetID,AssetLoggedDate,SerialNumber,SerialNumberIntraStage,SerialNumberCellComments,ESNumber,ESNumberAssigned,ESNumPiranaEntry,I2CID,DigitalIDOrigin,DecimalID,DigitalID,GenMK,SupportDocumentOrigin,CurrentPlantRevStatus,LabelLocation,MainComment,ESDPoint,IntrastageReady,CeMarkApplied,EquipmentManufacturerID,EquipmentStatusID,EquipmentGroupID,InternalLocationID,GeoLocationID")] EquipmentAsset equipmentAsset)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.EquipmentAssets.Add(equipmentAsset);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }

            ViewBag.EquipmentGroupID = new SelectList(db.EquipmentGroups, "EquipmentGroupID", "EquipmentGroupName", equipmentAsset.EquipmentGroupID);
            ViewBag.EquipmentManufacturerID = new SelectList(db.EquipmentManufacturers, "EquipmentManufacturerID", "EquipmentManufacturerName", equipmentAsset.EquipmentManufacturerID);
            ViewBag.EquipmentStatusID = new SelectList(db.EquipmentStatus, "EquipmentStatusID", "EquipmentStatusDescription", equipmentAsset.EquipmentStatusID);
            ViewBag.GeoLocationID = new SelectList(db.GeoLocations, "GeoLocationID", "GeoLocationName", equipmentAsset.GeoLocationID);
            ViewBag.InternalLocationID = new SelectList(db.InternalLocations, "InternalLocationID", "InternalLocationName", equipmentAsset.InternalLocationID);
            return View(equipmentAsset);
        }

        // GET: EquipmentAssets/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EquipmentAsset equipmentAsset = db.EquipmentAssets.Find(id);
            if (equipmentAsset == null)
            {
                return HttpNotFound();
            }
            ViewBag.EquipmentGroupID = new SelectList(db.EquipmentGroups, "EquipmentGroupID", "EquipmentGroupName", equipmentAsset.EquipmentGroupID);
            ViewBag.EquipmentManufacturerID = new SelectList(db.EquipmentManufacturers, "EquipmentManufacturerID", "EquipmentManufacturerName", equipmentAsset.EquipmentManufacturerID);
            ViewBag.EquipmentStatusID = new SelectList(db.EquipmentStatus, "EquipmentStatusID", "EquipmentStatusDescription", equipmentAsset.EquipmentStatusID);
            ViewBag.GeoLocationID = new SelectList(db.GeoLocations, "GeoLocationID", "GeoLocationName", equipmentAsset.GeoLocationID);
            ViewBag.InternalLocationID = new SelectList(db.InternalLocations, "InternalLocationID", "InternalLocationName", equipmentAsset.InternalLocationID);
            return View(equipmentAsset);
        }

        // POST: EquipmentAssets/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "EquipmentAssetID,AssetLoggedDate,SerialNumber,SerialNumberIntraStage,SerialNumberCellComments,ESNumber,ESNumberAssigned,ESNumPiranaEntry,I2CID,DigitalIDOrigin,DecimalID,DigitalID,GenMK,SupportDocumentOrigin,CurrentPlantRevStatus,LabelLocation,MainComment,ESDPoint,IntrastageReady,CeMarkApplied,EquipmentManufacturerID,EquipmentStatusID,EquipmentGroupID,InternalLocationID,GeoLocationID")] EquipmentAsset equipmentAsset)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(equipmentAsset).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }


            ViewBag.EquipmentGroupID = new SelectList(db.EquipmentGroups, "EquipmentGroupID", "EquipmentGroupName", equipmentAsset.EquipmentGroupID);
            ViewBag.EquipmentManufacturerID = new SelectList(db.EquipmentManufacturers, "EquipmentManufacturerID", "EquipmentManufacturerName", equipmentAsset.EquipmentManufacturerID);
            ViewBag.EquipmentStatusID = new SelectList(db.EquipmentStatus, "EquipmentStatusID", "EquipmentStatusDescription", equipmentAsset.EquipmentStatusID);
            ViewBag.GeoLocationID = new SelectList(db.GeoLocations, "GeoLocationID", "GeoLocationName", equipmentAsset.GeoLocationID);
            ViewBag.InternalLocationID = new SelectList(db.InternalLocations, "InternalLocationID", "InternalLocationName", equipmentAsset.InternalLocationID);
            return View(equipmentAsset);
        }

        // GET: EquipmentAssets/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            EquipmentAsset equipmentAsset = db.EquipmentAssets.Find(id);
            if (equipmentAsset == null)
            {
                return HttpNotFound();
            }
            return View(equipmentAsset);
        }

        // POST: EquipmentAssets/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            EquipmentAsset equipmentAsset = db.EquipmentAssets.Find(id);
            db.EquipmentAssets.Remove(equipmentAsset);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        public ActionResult NewAssetHelp2(AssetModel model, string returnUrl)
        {

            AssetModel assetmodel = new AssetModel();
            try
            {

                if (ModelState.IsValid)
                {
                    // Retrieve the perviously saved Profile
                    if (Session["AssetModel"] != null)
                        assetmodel = Session["AssetModel"] as AssetModel;

                    ModelState.AddModelError("", "error");
                }

            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }

            // If we got this far, something failed, redisplay form
            return PartialView(model);
        }

        public ActionResult NewAssetHelp(AssetModel model, string returnUrl)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    ModelState.AddModelError("", "error");
                }


            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }

            // If we got this far, something failed, redisplay form
            return PartialView();
        }

        public ActionResult Help(AssetModel model, string returnUrl)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    ModelState.AddModelError("", "error");
                }
            }
            catch (RetryLimitExceededException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
            }

            // If we got this far, something failed, redisplay form
            return PartialView();
        }
    }
}