quinta-feira, 23 de julho de 2015

SecuritiesController - MVC

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lapuinka.Securities.Models;

namespace Lapuinka.Securities.Controllers
{
    public class SecuritiesController : Controller
    {
        private LapuinkaSecuritiesDBContext db = new LapuinkaSecuritiesDBContext();

        //
        // GET: /Securities/

        public ViewResult Index()
        {
            var securities = db.Securities.Include(s => s.Vendor);
            return View(securities.ToList());
        }

        //
        // GET: /Securities/Details/5

        public ViewResult Details(int id)
        {
            Security security = db.Securities.Find(id);
            return View(security);
        }

        //
        // GET: /Securities/Create

        public ActionResult Create()
        {
            ViewBag.VendorID = new SelectList(db.Vendors, "ID", "Name");
            return View();
        }

        //
        // POST: /Securities/Create

        [HttpPost]
        public ActionResult Create(Security security)
        {
            if (ModelState.IsValid)
            {
                db.Securities.Add(security);
                db.SaveChanges();
                return RedirectToAction("Index"); 
            }

            ViewBag.VendorID = new SelectList(db.Vendors, "ID", "Name", security.VendorID);
            return View(security);
        }
       
        //
        // GET: /Securities/Edit/5

        public ActionResult Edit(int id)
        {
            Security security = db.Securities.Find(id);
            ViewBag.VendorID = new SelectList(db.Vendors, "ID", "Name", security.VendorID);
            return View(security);
        }

        //
        // POST: /Securities/Edit/5

        [HttpPost]
        public ActionResult Edit(Security security)
        {
            if (ModelState.IsValid)
            {
                db.Entry(security).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.VendorID = new SelectList(db.Vendors, "ID", "Name", security.VendorID);
            return View(security);
        }

        //
        // GET: /Securities/Delete/5

        public ActionResult Delete(int id)
        {
            Security security = db.Securities.Find(id);
            return View(security);
        }

        //
        // POST: /Securities/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {           
            Security security = db.Securities.Find(id);
            db.Securities.Remove(security);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

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

}

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Data.Entity;

namespace Lapuinka.Securities.Models
{

    public class Security
    {
        [Required]
        [Display(Name = "ID")]
        public int SecurityID { get; set; }

        [Required]
        [Display(Name = "Vendor Security ID")]
        public string VendorSecurityID { get; set; }

        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Name")]
        public string Name { get; set; }
       
        [Required]
        [DataType(DataType.Text)]
        [Display(Name = "Nome Completo")]
        public string NomeCompleto { get; set; }
       
        [Required]
        [Display(Name = "Vendor ID")]
        [ForeignKey("Vendor")]
        public int VendorID { get; set; }


        public virtual ICollection Prices { get; set; }
        public virtual Vendor Vendor { get; set; }

        // this could have been on any class, I stuck it here for ease of use in LINQPad    
        // caution reflection may be slow    
        public static object GetPropertyValue(object obj, string property)
        {
            // from Kjetil Watnedal on http://stackoverflow.com/questions/41244/dynamic-linq-orderby    
            System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property);    
            return propertyInfo.GetValue(obj, null);
        }
    }

}
Postar um comentário