Example #1
0
        public ActionResult Create(DonHang model)
        {
            if (ModelState.IsValid)
            {
                model.NgayGio = DateTime.Now;
                db.DonHangs.Add(model);
                db.SaveChanges();

                var GioHang = Session["GioHang"] as List <GioHang>;
                foreach (var monHang in GioHang)
                {
                    var gioHang = new GioHang();
                    gioHang.idDonHang = model.id;
                    gioHang.DonGia    = monHang.SanPham.DonGia;
                    gioHang.SoLuong   = 1;
                    gioHang.idSanPham = monHang.SanPham.id;
                    db.GioHangs.Add(gioHang);
                }
                db.SaveChanges();
                return(RedirectToAction("Index", "SanPham"));
            }
            else
            {
                return(View(model));
            }
        }
Example #2
0
        //Save the data in database either on creating or editing Product
        public JsonResult Create(ProductViewModel model)
        {
            var result = false;

            try
            {
                Product pro = new Product();
                pro.Id    = model.Id;
                pro.PName = model.PName;
                pro.Price = model.Price;
                if (model.Id == 0)
                {
                    db.Products.Add(pro);
                    db.SaveChanges();
                    result = true;
                }
                else
                {
                    db.Entry(pro).State = EntityState.Modified;
                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public IHttpActionResult PutItem(int id, Item item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != item.ItemID)
            {
                return(BadRequest());
            }

            db.Entry(item).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ItemExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #4
0
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != product.ProductID)
            {
                return(BadRequest());
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PutCustomer(int id, Customer customer)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != customer.CustomerID)
            {
                return(BadRequest());
            }

            db.Entry(customer).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!CustomerExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PostUser(userdetail data)
        {
            string message = "";

            if (data != null)
            {
                try
                {
                    objEntity.userdetails.Add(data);
                    int result = objEntity.SaveChanges();
                    if (result > 0)
                    {
                        message = "User has been sussfully added";
                    }
                    else
                    {
                        message = "faild";
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
            return(Ok(message));
        }
 public ActionResult AddUser(UserMaster obj)
 {
     try
     {
         if (obj.UserId <= 0)
         {
             obj.Created = DateTime.Now;
             context.UserMasters.Add(obj);
             context.SaveChanges();
             return(Json(obj));
         }
         else
         {
             UserMaster objuser = context.UserMasters.Find(obj.UserId);
             objuser.FirstName            = obj.FirstName;
             objuser.LastName             = obj.LastName;
             objuser.UserName             = obj.UserName;
             objuser.Email                = obj.Email;
             objuser.Modified             = DateTime.Now;
             context.Entry(objuser).State = EntityState.Modified;
             context.SaveChanges();
             return(Json(objuser));
         }
     }
     catch (Exception e)
     {
         throw;
     }
 }
Example #8
0
        //To Save data in database either on creating or editing Customer
        public JsonResult SaveDataInDatabase(CustomerViewModel model)
        {
            var result = false;

            try
            {
                //Save data on Editing the customer
                if (model.Id > 0)
                {
                    Customer Cus = db.Customers.SingleOrDefault(x => x.Id == model.Id);
                    Cus.CName   = model.CName;
                    Cus.Address = model.Address;
                    db.SaveChanges();
                    result = true;
                }
                //Save data on Adding the customer
                else
                {
                    Customer Cus = new Customer();
                    Cus.Id      = model.Id;
                    Cus.CName   = model.CName;
                    Cus.Address = model.Address;
                    db.Customers.Add(Cus);
                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(Json(result, JsonRequestBehavior.AllowGet));
        }
Example #9
0
 /// <summary>
 /// 執行新增或修改
 /// </summary>
 public void SaveOrInsert()
 {
     using (DemoEntities DemoEntities = new DemoEntities())
     {
         //將ViewModel 轉換成 EntityModel
         Employee EEmployee = Mapper.Map <EmployeeEditViewModel, Employee>(this);
         //如果有 EmployeeID 表示修改
         if (this.EmployeeID != 0)
         {
             DemoEntities.Entry(EEmployee).State = System.Data.Entity.EntityState.Modified;
             DemoEntities.SaveChanges();
         }
         else
         {
             //沒有 EmployeeID 表示新增
             try
             {
                 DemoEntities.Employee.Add(EEmployee);
                 DemoEntities.SaveChanges();
             }
             catch (Exception ex)
             {
                 throw;
             }
         }
     }
 }
Example #10
0
        //Save data in database either on creating or editing Sales
        public JsonResult Create(ProductSold model)
        {
            var result = false;

            try
            {
                if (model.Id > 0)      //Updating
                {
                    ProductSold pro = db.ProductSolds.SingleOrDefault(x => x.Id == model.Id);

                    if (model.ProductId == 0)   //Changes not made for this field
                    {
                        pro.ProductId = pro.ProductId;
                    }
                    else
                    {
                        pro.ProductId = model.ProductId;
                    }
                    if (model.CustomerId == 0)
                    {
                        pro.CustomerId = pro.CustomerId;
                    }
                    else
                    {
                        pro.CustomerId = model.CustomerId;
                    }
                    if (model.StoreId == 0)
                    {
                        pro.StoreId = pro.StoreId;
                    }
                    else
                    {
                        pro.StoreId = model.StoreId;
                    }
                    pro.DateSold        = model.DateSold;
                    db.Entry(pro).State = EntityState.Modified;
                    db.SaveChanges();
                    result = true;
                }
                else     //Adding
                {
                    ProductSold pro = new ProductSold();
                    pro.ProductId  = model.ProductId;
                    pro.CustomerId = model.CustomerId;
                    pro.StoreId    = model.StoreId;
                    pro.DateSold   = model.DateSold;
                    db.ProductSolds.Add(pro);
                    db.SaveChanges();
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));
        }
        public ActionResult Create([Bind(Include = "DepartmentID,Name")] Department department)
        {
            if (ModelState.IsValid)
            {
                db.Departments.Add(department);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(department));
        }
Example #12
0
        public ActionResult Create([Bind(Include = "id,name,age")] Client client)
        {
            if (ModelState.IsValid)
            {
                db.Client.Add(client);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(client));
        }
Example #13
0
        public ActionResult Create([Bind(Include = "ChequeID,Bank,Branch,Cheque_No,Amount")] cheque cheque)
        {
            if (ModelState.IsValid)
            {
                db.cheques.Add(cheque);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(cheque));
        }
        public ActionResult Create([Bind(Include = "EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO")] EMPDATA eMPDATA)
        {
            if (ModelState.IsValid)
            {
                db.EMPDATAs.Add(eMPDATA);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(eMPDATA));
        }
Example #15
0
        public ActionResult Create([Bind(Include = "EmployeeID,Name,DepartmentID,Address")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.DepartmentID = new SelectList(db.Departments, "DepartmentID", "Name", employee.DepartmentID);
            return(View(employee));
        }
Example #16
0
 //新增圖檔
 public bool Create(tb_image oImage)
 {
     try
     {
         _db.DbSetImage.Add(oImage);
         _db.SaveChanges();
         return(true);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Example #17
0
 public ActionResult Create(KichCo model)
 {
     if (ModelState.IsValid)
     {
         db.KichCoes.Add(model);
         db.SaveChanges();
         return(RedirectToAction("List"));
     }
     else
     {
         return(View(model));
     }
 }
Example #18
0
 public ActionResult Add(StudentModel model)
 {
     if (ModelState.IsValid)
     {
         Student s = new Student();
         s.Name    = model.Name;
         s.Age     = model.Age;
         s.State   = model.State;
         s.Country = model.Country;
         entity.Students.Add(s);
         entity.SaveChanges();
     }
     return(PartialView("_Detail", entity.Students.ToList()));
 }
Example #19
0
        public ActionResult Create([Bind(Include = "id,name,meta_name")] tbl_Slider tbl_Slider)
        {
            if (Session["admin"] == null)
            {
                return(RedirectToAction("Login"));
            }
            if (ModelState.IsValid)
            {
                db.tbl_Slider.Add(tbl_Slider);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(tbl_Slider));
        }
        public ActionResult Create([Bind(Include = "id,username,pass,name")] tbl_Admin tbl_Admin)
        {
            if (Session["admin"] == null)
            {
                return(RedirectToAction("Login"));
            }
            if (ModelState.IsValid)
            {
                db.tbl_Admin.Add(tbl_Admin);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(tbl_Admin));
        }
        public ActionResult Create([Bind(Include = "id,username,pass,name,email,phone,cus_address")] tbl_Customer tbl_Customer)
        {
            if (Session["admin"] == null)
            {
                return(RedirectToAction("Login"));
            }
            if (ModelState.IsValid)
            {
                db.tbl_Customer.Add(tbl_Customer);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(tbl_Customer));
        }
Example #22
0
        public IHttpActionResult SaveInformation(Models.InformationView info)
        {
            IList <Models.InformationView> info1 = null;

            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid data."));
            }

            using (var ctx = new DemoEntities())
            {
                ctx.InforMation.Add(new InforMation()
                {
                    Id      = info.Id,
                    Name    = info.Name,
                    Address = info.Address,
                    Mobile  = info.Mobile,
                    Email   = info.Email
                });

                ctx.SaveChanges();

                info1 = ctx.InforMation.Select(s => new Models.InformationView()
                {
                    Id      = s.Id,
                    Name    = s.Name,
                    Address = s.Address,
                    Mobile  = s.Mobile,
                    Email   = s.Email
                }).ToList <Models.InformationView>();
            }

            return(Ok(info1));
        }
        public object updateRecord(UpdateGenericFormModel form)
        {
            DateTime today = DateTime.Today;

            try
            {
                using (DemoEntities db = new DemoEntities())
                {
                    // Read the form data.
                    GenericTable updateRecordForm = db.GenericTables.Find(form.Id);
                    updateRecordForm.Text       = form.Text;
                    updateRecordForm.Number     = int.Parse(form.Number);
                    updateRecordForm.Boolean    = Boolean.Parse(form.Boolean);
                    updateRecordForm.JSDate     = DateTime.Parse(form.JSDate);
                    updateRecordForm.JSDateTime = today;
                    db.SaveChanges();

                    return(new { status = StatusCodes.OK.code, msg = "Successfully Updated Record" });
                }
            }
            catch (System.Exception e)
            {
                return(new { status = StatusCodes.NotFound.code, msg = e.InnerException, data = 0 });
            }
        }
        public ActionResult Create([Bind(Include = "id,level_status,cus_id,created_at,total_price,cus_name")] tbl_Bill tbl_Bill)
        {
            if (Session["admin"] == null)
            {
                return(RedirectToAction("Login"));
            }
            if (ModelState.IsValid)
            {
                db.tbl_Bill.Add(tbl_Bill);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.cus_id = new SelectList(db.tbl_Customer, "id", "username", tbl_Bill.cus_id);
            return(View(tbl_Bill));
        }
        public object addRecord(GenericFormModel form)
        {
            DateTime today = DateTime.Today;

            try
            {
                using (DemoEntities db = new DemoEntities())
                {
                    // Read the form data.

                    GenericTable addRecordForm = new GenericTable();
                    addRecordForm.Text    = form.Text;
                    addRecordForm.Number  = int.Parse(form.Number);
                    addRecordForm.Boolean = Boolean.Parse(form.Boolean);
                    addRecordForm.JSDate  = DateTime.Parse(form.JSDate);
                    //addRecordForm.JSDateTime = DateTime.Parse(form.JSDateTime);

                    db.GenericTables.Add(addRecordForm);
                    db.SaveChanges();
                    return(new { status = StatusCodes.OK.code, msg = "Successfully Added Record To Database" });
                }
            }
            catch (System.Exception e)
            {
                return(new { status = StatusCodes.NotFound.code, msg = e.InnerException, data = 0 });
            }
        }
Example #26
0
        private void UpdateCustomer(GridViewRow row)
        {
            var txtTitle       = row.FindControl("txtTitle") as TextBox;
            var txtFirstName   = row.FindControl("txtFirstName") as TextBox;
            var txtLastName    = row.FindControl("txtLastName") as TextBox;
            var txtDateOfBirth = row.FindControl("txtDateOfBirth") as TextBox;
            var ddlGender      = row.FindControl("ddlGender") as DropDownList;

            var id       = int.Parse(gridViewCustomers.DataKeys[row.RowIndex].Value.ToString());
            var customer = Customers.FirstOrDefault(x => x.Id == id);

            if (customer == null)
            {
                ValidationMessages.Add("Unable to locate customer record to update");
                return;
            }

            using (var entities = new DemoEntities())
            {
                entities.Customers.Attach(customer);
                customer.Title       = txtTitle.Text;
                customer.FirstName   = txtFirstName.Text;
                customer.LastName    = txtLastName.Text;
                customer.DateOfBirth = DateTime.Parse(txtDateOfBirth.Text);
                customer.GenderId    = int.Parse(ddlGender.SelectedValue);
                entities.SaveChanges();
            }
        }
        public ActionResult Create([Bind(Include = "id,pub_id,name,meta_name,amount")] tbl_Category tbl_Category)
        {
            if (Session["admin"] == null)
            {
                return(RedirectToAction("Login"));
            }
            if (ModelState.IsValid)
            {
                db.tbl_Category.Add(tbl_Category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.pub_id = new SelectList(db.tbl_Publisher, "id", "name", tbl_Category.pub_id);
            return(View(tbl_Category));
        }
        public ActionResult Register(TblTweetuser user)
        {
            DemoEntities demo = new DemoEntities();

            demo.TblTweetusers.Add(user);
            demo.SaveChanges();
            string message = string.Empty;

            switch (user.UserId)
            {
            case -1:
                message = "Username already exists.\\nPlease choose a different username.";
                break;

            case -2:
                message = "Supplied email address has already been used.";
                break;

            default:
                message = "Registration successful.\\nUser Id: " + user.UserId.ToString();
                break;
            }
            ViewBag.Message = message;
            // usersEntities.dis
            ObjectResult <TblTweet> list = demo.DisplayTweets("%," + user.UserId.ToString() + ",%");
            Tweets tuser = TweetLists(user.UserId.ToString(), user.UserName, list);

            ViewBag.UserName = user.UserName;
            ViewBag.UserId   = user.UserId.ToString();
            return(View("Home", tuser));
        }
        public IHttpActionResult EditStudent(Sinh_Vien student)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new DemoEntities())
            {
                var existingStudent = ctx.Sinh_Vien.Where(s => s.MSV == student.MSV)
                                      .FirstOrDefault <Sinh_Vien>();

                if (existingStudent != null)
                {
                    existingStudent.Ho  = student.Ho;
                    existingStudent.Ten = student.Ten;

                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }
Example #30
0
        //Put Method
        public IHttpActionResult Put(Models.InformationView info)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            using (var ctx = new DemoEntities())
            {
                var eInfo = ctx.InforMation.Where(s => s.Id == info.Id)
                            .FirstOrDefault <InforMation>();

                if (eInfo != null)
                {
                    eInfo.Name = info.Name;


                    ctx.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok());
        }