public ActionResult EditActivity(ActivityVM newActivity) { try { var oldActivity = _activityManager.RetrieveActivityByID(newActivity.ActivityID); _activityManager.EditActivity(oldActivity, newActivity); _scheduleManager.EditActivitySchedule(oldActivity, newActivity); return(RedirectToAction("ActivityList", new { message = "Activity Updated." })); } catch (Exception) { ViewBag.ActivityTypeList = _activityManager.RetrieveAllActivityTypes(); return(View()); } }
// This event handler fires when the save button is clicked. It gathers all the information from the text boxes and then, based upon if _addMode is true or false, // either updates an activity or creates a new one. private void BtnSave_Click(object sender, RoutedEventArgs e) { ActivityVM activityToSave = new ActivityVM(); if (txtActivityName.Text == "") { MessageBox.Show("You must enter an activity name."); txtActivityName.Focus(); return; } if (txtAddress1.Text == "") { MessageBox.Show("You must enter an address."); txtAddress1.Focus(); return; } if (txtCity.Text == "") { MessageBox.Show("You must enter a city name."); txtCity.Focus(); return; } if (txtDescription.Text == "") { MessageBox.Show("You must enter a description."); txtDescription.Focus(); return; } if (txtEnd.Text == "") { MessageBox.Show("You must enter a date and time."); txtEnd.Focus(); return; } try { DateTime end = DateTime.Parse(txtEnd.Text); } catch (Exception ex) { MessageBox.Show("You must enter valid date and time."); txtEnd.Focus(); return; } if (txtLocation.Text == "") { MessageBox.Show("You must enter a location"); txtLocation.Focus(); return; } if (txtStart.Text == "") { MessageBox.Show("You must enter a date and time"); txtStart.Focus(); return; } try { DateTime start = DateTime.Parse(txtStart.Text); } catch (Exception) { MessageBox.Show("You must enter valid date and time."); txtEnd.Focus(); return; } if (txtState.Text == "") { MessageBox.Show("You must enter a state"); txtState.Focus(); return; } if (txtZip.Text == "") { MessageBox.Show("You must enter a Zip code"); txtZip.Focus(); return; } if (cmbActivityType == null) { MessageBox.Show("You must choose a Activity Type"); cmbActivityType.Focus(); return; } try { ActivityVM newActivity = new ActivityVM() { ActivityName = txtActivityName.Text, ActivityTypeID = cmbActivityType.SelectedItem.ToString(), LocationName = txtLocation.Text, Address1 = txtAddress1.Text, Address2 = txtAddress2.Text, City = txtCity.Text, State = txtState.Text, Zip = txtZip.Text, Description = txtDescription.Text, End = DateTime.Parse(txtEnd.Text), Start = DateTime.Parse(txtStart.Text), }; if (_addMode) { int activityID = _activityManager.AddActivity(newActivity); _activityManager.AddActivitySchedule(activityID, DateTime.Parse(txtStart.Text), DateTime.Parse(txtEnd.Text)); MessageBox.Show("Activity: " + newActivity.ActivityName + " created."); _addMode = false; populateActivityList(); btnSave.Visibility = Visibility.Hidden; } else { newActivity.ActivityID = _activity.ActivityID; _activityManager.EditActivity(_activity, newActivity); _scheduleManager.EditActivitySchedule(_activity, newActivity); populateUserActivityList(); populateActivityList(); btnSave.Visibility = Visibility.Hidden; btnEdit.Visibility = Visibility.Visible; } } catch (Exception ex) { MessageBox.Show(ex.Message); } }