public ContactPage() { InitializeComponent(); SVM = new ShelterViewModel(); //listShelters.ItemsSource = SVM.Shelters; //In XAML the Binding item source is done BindingContext = SVM; }
public ActionResult Shelter(ShelterViewModel vm) { if (ModelState.IsValid) { // this is needed for some reason.. come back to it // http://stackoverflow.com/questions/4837744/hiddenfor-not-getting-correct-value-from-view-model ModelState.Remove("SiteID"); // find out if the soup kitchen exists for this SiteID already // and set up the SQL to INSERT or UPDATE accordingly if (vm.SiteID.Equals(0)) { // we didn't find an existing soup kitchen. so insert a new one based on the logged in users Site ID int?SiteID = Session["SiteID"] as int?; //insert shelter for site string sql = String.Format( "INSERT INTO shelter (SiteID, MaleBunksAvailable, FemaleBunksAvailable, MixedBunksAvailable, RoomsAvailable, HoursOfOperation, ConditionsForUse, Description) " + "VALUES ({0}, {1}, {2}, {3}, {4}, '{5}', '{6}', '{7}'); ", SiteID.Value.ToString(), vm.MaleBunksAvailable, vm.FemaleBunksAvailable, vm.MixedBunksAvailable, vm.RoomsAvailable, vm.HoursOfOperation, vm.ConditionsForUse, vm.Description); SqlHelper.ExecuteNonQuery(sql); vm.SiteID = SiteID.Value; // set the ID since it now exists vm.StatusMessage = "Succesfully added!"; } else { // update the existing record string sql = String.Format( "UPDATE shelter " + "SET MaleBunksAvailable = {0}, " + "FemaleBunksAvailable = {1}, " + "MixedBunksAvailable = {2}, " + "RoomsAvailable = {3}, " + "HoursOfOperation = '{4}', " + "ConditionsForUse = '{5}', " + "Description = '{6}' " + "WHERE SiteID = {7} ;", vm.MaleBunksAvailable, vm.FemaleBunksAvailable, vm.MixedBunksAvailable, vm.RoomsAvailable, vm.HoursOfOperation, vm.ConditionsForUse, vm.Description, vm.SiteID); SqlHelper.ExecuteNonQuery(sql); vm.StatusMessage = "Succesfully updated!"; } } return(View(vm)); }
public ActionResult Shelter() { // Get the logged in Site ID from the session int?SiteID = Session["SiteID"] as int?; // if there is none, redirect to the login page if (!SiteID.HasValue) { return(RedirectToAction("Login", "Account")); } // set up the response object ShelterViewModel vm = new ShelterViewModel(); // find shelter's for user's site string sql = String.Format( "SELECT MaleBunksAvailable, FemaleBunksAvailable, MixedBunksAvailable, RoomsAvailable, HoursOfOperation, ConditionsForUse, Description " + "FROM shelter WHERE SiteID = {0}; ", SiteID.Value.ToString()); // run the sql against the db object[] result = SqlHelper.ExecuteSingleSelect(sql, 7); // if we got a result, populate the view model fields if (result != null) { vm.SiteID = SiteID.Value; vm.MaleBunksAvailable = int.Parse(result[0].ToString()); vm.FemaleBunksAvailable = int.Parse(result[1].ToString()); vm.MixedBunksAvailable = int.Parse(result[2].ToString()); vm.RoomsAvailable = int.Parse(result[3].ToString()); vm.HoursOfOperation = result[4].ToString(); vm.ConditionsForUse = result[5].ToString(); vm.Description = result[6].ToString(); } return(View(vm)); }