Пример #1
0
        /// <summary>
        /// The following method reads the uploaded .csv file
        /// and adds them to the Orders and Tickets tables within
        /// the database. This method also displays a table of the
        /// newly uploaded data from the .csv
        /// </summary>
        /// <returns>The index.</returns>
        /// <param name="upload">The file uploaded.</param>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Index(IFormFile upload)
        {
            if (ModelState.IsValid)
            {

                if (upload != null && upload.Length > 0)
                {

                    if (upload.FileName.EndsWith(".csv", StringComparison.Ordinal))
                    {
                        Stream stream = upload.OpenReadStream();
                        DataTable csvTable = new DataTable();
                        using (CsvReader csvReader =
                            new CsvReader(new StreamReader(stream), true))
                        {
                            csvTable.Load(csvReader);

                            //Add to database
                            foreach(DataRow row in csvTable.Rows)
                            {
                                Orders order = new Orders();
                                Tickets ticket = new Tickets();
                                order.FirstName = row[1].ToString();
                                order.LastName = row[2].ToString();
                                ticket.TicketNumber = row[3].ToString();
                                ticket.EventDate = DateTime.Parse(row[4].ToString());

                                //Add to order table
                                _orderContext.Add(order);
                                _orderContext.SaveChangesAsync();

                                //Add to ticket table
                                _ticketContext.Add(ticket);
                                _ticketContext.SaveChangesAsync();
                            }
                        }
                        //Return data to the view.
                        return View(csvTable);
                    }
                    else
                    {
                        ModelState.AddModelError("File", "This file format is not supported. Try again.");
                        return View();
                    }
                }
                else
                {
                    ModelState.AddModelError("File", "Please Upload Your file");
                }
            }
            return View();
        }

Пример #2
0
        public async Task <IActionResult> Create([Bind("ID,TicketNumber,EventDate")] Tickets tickets)
        {
            if (ModelState.IsValid)
            {
                _context.Add(tickets);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(tickets));
        }
Пример #3
0
        public async Task <IActionResult> Create([Bind("ID,FirstName,LastName")] Orders orders)
        {
            if (ModelState.IsValid)
            {
                _context.Add(orders);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(orders));
        }