public async Task <IActionResult> Create([Bind("MaLoai,TenLoai,MaLoaiCha")] Loai loai) { try { _context.Add(loai); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (Exception) { ViewData["MaLoaiCha"] = new SelectList(_context.Loais, "MaLoai", "TenLoai", loai.MaLoaiCha); return(View(loai)); } }
public async Task <IActionResult> Create([Bind("MaHH,TenHH,Hinh,MoTa,DonGia,SoLuong,MaLoai,NgayDang,NoiBat")] HangHoa hangHoa, IFormFile fHinh) { if (fHinh != null) { var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Hinh", fHinh.FileName); using (var file = new FileStream(path, FileMode.Create)) { await fHinh.CopyToAsync(file); } hangHoa.Hinh = fHinh.FileName; } hangHoa.NgayDang = DateTime.Now; if (ModelState.IsValid) { _context.Add(hangHoa); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["MaLoai"] = new SelectList(_context.loais, "MaLoai", "TenLoai", hangHoa.MaLoai); return(View(hangHoa)); }
public async Task <IActionResult> Create([Bind("Id,LabelId,BodyId,ColorId,Year,TransmissionId,Price,LandlordId,GovNumber")] Cars cars) { if (ModelState.IsValid) { _context.Add(cars); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["BodyId"] = new SelectList(_context.Bodies, "BodyId", "Body", cars.BodyId); ViewData["ColorId"] = new SelectList(_context.Colors, "ColorId", "Color", cars.ColorId); ViewData["LabelId"] = new SelectList(_context.Labels, "LableId", "Lable", cars.LabelId); ViewData["LandlordId"] = new SelectList(_context.Landlords, "Id", "Name", cars.LandlordId); ViewData["TransmissionId"] = new SelectList(_context.Transmissions, "TransmissionId", "Trasmission", cars.TransmissionId); return(View(cars)); }
public IActionResult InsertFaculty([FromQuery] Faculty fac) { Faculty new_fac = new Faculty { name = fac.name, phone = fac.phone, email = fac.email, address = fac.address, qualification = fac.qualification }; _context.Add(new_fac); _context.SaveChanges(); return(Content("Changes Saved")); }
public IActionResult Index() { myDBContext.Add(new Book() { Name = "三国演义", Content = "三国", AutherID = 1, DateTime = DateTime.Now }); myDBContext.SaveChanges(); ViewBag.Message = "OK!"; var list = myDBContext.Books; return(View(list)); }
public IActionResult Create([Bind] Users users) { var user_role = HttpContext.Session.GetString("role_user"); if (user_role == "Admin") { if (ModelState.IsValid) { _context.Add(users); _context.SaveChanges(); return(RedirectToAction("Index", "User", new { area = "Accounts" })); } return(View()); } return(RedirectToAction("Index", "Home", new { area = "" })); }
public async Task<IActionResult> Create([Bind("MaTK,TenDangNhap,MatKhau")] TaiKhoan taiKhoan) { TaiKhoan username = _context.TaiKhoans.SingleOrDefault(p => p.TenDangNhap == taiKhoan.TenDangNhap); if (username != null) { ViewBag.UsernameErr = "Username này đã tồn tại"; } if (ViewBag.UsernameErr == null) { if (ModelState.IsValid) { _context.Add(taiKhoan); await _context.SaveChangesAsync(); return RedirectToAction(nameof(Index)); } } return View(taiKhoan); }
public async Task <IActionResult> Create([Bind("Id,MaHH,TenHH,SoLuong,DonGia,MoTa,Hinh,GiamGia,MaLoai")] HangHoa hangHoa, IFormFile Hinh) { if (ModelState.IsValid) { if (Hinh != null) { hangHoa.Hinh = MyTools.ProcessUpoadHinh(Hinh, "HangHoa"); } hangHoa.Id = Guid.NewGuid(); _context.Add(hangHoa); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["MaLoai"] = new SelectList(_context.Loais, "MaLoai", "TenLoai", hangHoa.MaLoai); return(View(hangHoa)); }
public async Task <IActionResult> Create([Bind("ID,TieuDe,NoiDung,Hinh,MaLoai")] BaiViet baiViet, IFormFile fHinh) { if (fHinh != null) { var path = Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Hinh", fHinh.FileName); using (var file = new FileStream(path, FileMode.Create)) { await fHinh.CopyToAsync(file); } baiViet.Hinh = fHinh.FileName; } if (ModelState.IsValid) { _context.Add(baiViet); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(baiViet)); }
public IActionResult InsertStudent([FromQuery] Student std) { Student new_std = new Student { roll = std.roll, full_name = std.full_name, cnic = std.cnic, phone = std.phone, credits_attempted = std.credits_attempted, credits_earned = std.credits_earned, dob = std.dob, email = std.email, address = std.address, status = std.status }; _context.Add(new_std); _context.SaveChanges(); return(Content("Changes Saved")); }
public void Create(T entity) { context.Add(entity); Save(); }
public async Task <IActionResult> Register([Bind("UserName", "Password", "FirstName", "LastName", "Sex", "Address", "Email", "PhoneNumber")] UserRegister userRegister, [Bind("TwoFactorCheck")] bool TwoFactorCheck, [Bind("fFile")] IFormFile fFile) { Customer customerSimilar = _ctx.Customer.AsNoTracking().FirstOrDefault(p => p.UserName == userRegister.UserName); if (customerSimilar == null) { Customer customer = _mapper.Map <UserRegister, Customer>(userRegister); if (ModelState.IsValid) { //thiếu check mail, phone,... customer.Password = MyHashTool.GetMd5Hash(customer.Password); //thêm ảnh đại diện string fileName = UploadAnh(fFile); if (fileName != null) { customer.Image = fileName; } else { customer.Image = ""; } customer.IsActive = true; Roles role = _ctx.Roles.AsNoTracking().SingleOrDefault(p => p.RoleName == "Customer"); customer.Role = role.RoleId; customer.PhoneNumberConfirmed = false; UserModel userModel = new UserModel { Email = customer.Email, CountryCode = "+84", PhoneNumber = (customer.PhoneNumber.Length > 9) ? customer.PhoneNumber.Substring(1) : customer.PhoneNumber }; //Lấy authy id var authyId = await _authy.RegisterUserAsync(userModel).ConfigureAwait(false); _ctx.Add(customer); _ctx.SaveChanges(); if (string.IsNullOrEmpty(authyId)) { //return Json(new { success = false }); ViewBag.RegisterResult = "Đăng ký thành công"; ViewBag.RegisterSMS = "Xác thực số điện thoại thất bại"; return(View("Login")); } else { //update authyId in database customer.AuthyId = authyId; _ctx.Update(customer); await _ctx.SaveChangesAsync(); if (TwoFactorCheck == true) { ViewBag.CustomerId = customer.CustomerId; // Gửi sms mã xác nhận var sendSMSResponse = await _authy.SendSmsAsync(customer.AuthyId).ConfigureAwait(false); if (sendSMSResponse.StatusCode == HttpStatusCode.OK) { var smsVerificationSucceedObject = JsonConvert.DeserializeObject <AccessCodeVerifyResult>(await sendSMSResponse.Content.ReadAsStringAsync()); if (smsVerificationSucceedObject.Success) { ViewBag.CustomerId = customer.CustomerId; ViewBag.ResultSMS = "Gửi mã thành công!"; //Send SMS success return(View("VerifyUser")); } else { ViewBag.ResultSMS = "Gửi mã thất bại!"; ViewBag.CustomerId = customer.CustomerId; //Fail return(View("VerifyUser")); } } else { ViewBag.ResultSMS = "Gửi mã thất bại!"; return(View("Login")); } } else { ViewBag.RegisterResult = "Đăng ký thành công"; return(View("Login")); } } } } ViewBag.RegisterResult = "Trùng tên đăng nhập hoặc mật khẩu"; return(View()); }
public async Task <IActionResult> Create([Bind("Name,Address,Phone,Email,PayMethod,ShipMethod,ShipCost,Comment,OrderStatus,ShipDate")] Orders order, [Bind("ArrAddItemId")] string ArrAddItemId, [Bind("ArrAddItemQuantity")] string ArrAddItemQuantity) { var emp = HttpContext.Session.GetObject <Employee>("Employee"); if (emp == null) { return(RedirectToAction("Index", "Login")); } //mảng id item thêm string[] arrAddItemId = new string[] { }; if (ArrAddItemId.Length > 0) { ArrAddItemId = ArrAddItemId.Trim(); arrAddItemId = ArrAddItemId.Split(','); } //mảng quantity item sửa string[] arrAddItemQuantity = new string[] { }; if (ArrAddItemQuantity.Length > 0) { ArrAddItemQuantity = ArrAddItemQuantity.Trim(); arrAddItemQuantity = ArrAddItemQuantity.Split(','); } if (ModelState.IsValid) { try { _context.Add(order); _context.SaveChanges(); int?total = 0; if (ArrAddItemId.Count() > 0) { for (var i = 0; i < arrAddItemId.Count(); ++i) { try { Product p = _context.Product.AsNoTracking().SingleOrDefault(o => o.ProductId == int.Parse(arrAddItemId[i].Trim())); if (p != null) { OrderDetail detail = new OrderDetail(); detail.OrderId = order.OrderId; detail.ProductId = p.ProductId; detail.Quantity = int.Parse(arrAddItemQuantity[i].Trim()); detail.Price = (p.Discount == 0) ? p.Price : p.PromotionPrice; _context.Update(detail); total += detail.Price * detail.Quantity; } } catch (Exception) { } } } order.EmployeeId = emp.EmployeeId; order.Total = total + (int)order.ShipCost; order.CreatedDate = DateTime.Now; _context.Update(order); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OrdersExists(order.OrderId)) { return(NotFound()); } else { throw; } } } var orderDetails = await _context.OrderDetail.Where(od => od.OrderId == order.OrderId).Include(p => p.Product).ToListAsync(); ViewBag.orderDetails = orderDetails; ViewData["EmployeeName"] = emp.FirstName + " " + emp.LastName; return(View("Edit", order)); }
public Student save([FromBody] Student student) { dbservice.Add(student); dbservice.SaveChanges(); return(student); }