public async Task <IActionResult> Create([Bind("ID,Faces,Gender")] Picture picture) { if (ModelState.IsValid) { _context.Add(picture); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(picture)); }
// Delete image and all it's details public async void deleteImage(int id, string pictureName) { // Get access to database using (var db = new PictureContext()) { // Find id in database var image = db.Pictures.Single(i => i.pictureId == id); // Remove data from database db.Remove(image); // Save chanages to database await db.SaveChangesAsync(); // ===== DELETE FILE FROM LOCAL STORAGE ===== try { // Point to the local storage folder StorageFile deleteFile = await ApplicationData.Current.LocalFolder.GetFileAsync(pictureName); // If file exists then delete it if (deleteFile != null) { await deleteFile.DeleteAsync(); } } catch (System.IO.FileNotFoundException) { } } // End using } // End deleteImage
}// End method SearchForFile // This method saves the image path and its label to the database private void SaveToDB_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // Get access to resources for localisation var loader = new Windows.ApplicationModel.Resources.ResourceLoader(); // Retrieve reqired strings var textValidation = loader.GetString("ImageDetailsTextValidation"); var noLabelValidation = loader.GetString("ImageDetailsNoLabelValidation"); // Get text from label textbox string label = Label.Text; // Check that string is 20 characters or less if (label.Length <= 20) { // ---------- Adapted from https://msdn.microsoft.com/en-us/library/system.string.isnullorempty(v=vs.110).aspx ---------- // If the label textbox has text in it if (!String.IsNullOrEmpty(label)) { // ========== SAVE TO DATABASE ========== // Get access to the database using (var db = new PictureContext()) { // Store image path and label into model var picture = new Picture { picturePath = imagePath, pictureLabel = label }; // Add picture to table db.Pictures.Add(picture); // Save the database with the new picture db.SaveChangesAsync(); }// End using // Clear the textbox label = ""; // Navigate back to main page this.Frame.Navigate(typeof(MainPage)); }// End if else { // Display message to user saying the picture must have a label TextValidation.Text = noLabelValidation; }// End if (String is empty or not) } else { // Display message to user saying it must be 20 characters or less TextValidation.Text = textValidation; }// End if (string is 20 characters or not) // Clear the textbox label = ""; }// End SaveToDB_Click
} // End deleteImage // Update the label in the database public async void updateLabel(int id, string newLabel) { // Get access to database using (var db = new PictureContext()) { // Find id in database var label = db.Pictures.Single(i => i.pictureId == id); // Update the label label.pictureLabel = newLabel; // Save changes await db.SaveChangesAsync(); } //End using } // End updateLabel