Tuesday, February 11, 2014

How To Create Dynamic Dropdownlist in MVC 5 EF 6

*** Just Follow the Bold Lines...


Table:

create table tblInsPlans
(
    InsID        Numeric(11),
    InsDueDate     Datetime,
    InsSeq         Numeric(3),
    InsAmt         Numeric(11,2),
    PaidAmt        Numeric(11,2),
    HeadID         Numeric(3),--FK
    Remarks     Varchar(100),
    InsertedBy     VARCHAR(15) null,
    InsertedOn     DATETime  null,
    UpdatedBy      VARCHAR(15) null,
    UpdatedOn      DATETime  null,
    IsActive       bit not null DEFAULT(0),
    IsDeleted     bit not null DEFAULT(0),

    primary    key(InsID),
    FOREIGN KEY(HeadID) REFERENCES tblColHeads(HeadID)
);

 

Model:

 public class tblInsPlan
    {

        [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]

        [DisplayName("Install ID")]
        public decimal InsID { get; set; }
         [DisplayName("Install Due Date")]
        public DateTime? InsDueDate { get; set; }
         [DisplayName("Install Sequence")]
         public decimal InsSeq { get; set; }
         [DisplayName("Install Amount")]
         public decimal InsAmt { get; set; }
         [DisplayName("Paid Amount")]
         public decimal PaidAmt { get; set; }
         [DisplayName("Head ID")]
         public decimal HeadID { get; set; }
        [DisplayName("Remarks")]
         public decimal Remarks { get; set; }
        [DisplayName("Inserted By")]
        public string InsertedBy { get; set; }
        [DisplayName("Inserted On")]
        public DateTime? InsertedOn { get; set; }
        [DisplayName("Updated By")]
        public string UpdatedBy { get; set; }
        [DisplayName("Updated On")]
        public DateTime? UpdatedOn { get; set; }
        [DisplayName("Is Active")]
        public bool IsActive { get; set; }
        [DisplayName("Is Deleted")]
        public bool IsDeleted { get; set; }

        public virtual tblColHead tblColHead { get; set; }

    }

Controller:

 public class InsPlanController : Controller
    {
        private BLRSDBEntities db = new BLRSDBEntities();

        // GET: /InsPlan/
        public ActionResult Index()
        {

            var tblinsplans = db.tblInsPlans.Include(t => t.tblColHead);

            return View(tblinsplans.ToList());

        }

        // GET: /InsPlan/Details/5
        public ActionResult Details(decimal id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblInsPlan tblinsplan = db.tblInsPlans.Find(id);
            if (tblinsplan == null)
            {
                return HttpNotFound();
            }
            return View(tblinsplan);
        }

        // GET: /InsPlan/Create
        public ActionResult Create()
        {

            ViewBag.HeadID = new SelectList(db.tblColHeads, "HeadID", "Title");

            return View();
        }

        // POST: /InsPlan/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="InsID,InsDueDate,InsSeq,InsAmt,PaidAmt,HeadID,Remarks,InsertedBy,InsertedOn,UpdatedBy,UpdatedOn,IsActive,IsDeleted")] tblInsPlan tblinsplan)
        {
            if (ModelState.IsValid)
            {
              //  tblinsplan.InsID = Common.GetMaxIDInt("tblInsPlans", "InsID");
             //   tblinsplan.InsertedBy = "Mn";
             //   tblinsplan.InsertedOn = System.DateTime.Now;
                db.tblInsPlans.Add(tblinsplan);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.HeadID = new SelectList(db.tblColHeads, "HeadID", "Title", tblinsplan.HeadID);

            return View(tblinsplan);
        }

        // GET: /InsPlan/Edit/5
        public ActionResult Edit(decimal id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblInsPlan tblinsplan = db.tblInsPlans.Find(id);
            if (tblinsplan == null)
            {
                return HttpNotFound();
            }

            ViewBag.HeadID = new SelectList(db.tblColHeads, "HeadID", "Title", tblinsplan.HeadID);

            return View(tblinsplan);

        }

        // POST: /InsPlan/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include="InsID,InsDueDate,InsSeq,InsAmt,PaidAmt,HeadID,Remarks,InsertedBy,InsertedOn,UpdatedBy,UpdatedOn,IsActive,IsDeleted")] tblInsPlan tblinsplan)
        {
            if (ModelState.IsValid)
            {
            //    tblinsplan.UpdatedBy = "Mamun";
             //   tblinsplan.UpdatedOn = System.DateTime.Now;
                db.Entry(tblinsplan).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.HeadID = new SelectList(db.tblColHeads, "HeadID", "Title", tblinsplan.HeadID);

            return View(tblinsplan);
        }

        // GET: /InsPlan/Delete/5
        public ActionResult Delete(decimal id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            tblInsPlan tblinsplan = db.tblInsPlans.Find(id);
            if (tblinsplan == null)
            {
                return HttpNotFound();
            }
            return View(tblinsplan);
        }

        // POST: /InsPlan/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(decimal id)
        {
            tblInsPlan tblinsplan = db.tblInsPlans.Find(id);
            db.tblInsPlans.Remove(tblinsplan);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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

    }

No comments:

Post a Comment