public ActionResult UpdateWeightLog(WeightLogModel weightLog) { //check if the model state is valid or not if (ModelState.IsValid) { //update data in the database based on WeightId impl.UpdateWeight(weightLog.WeightId, weightLog); //redirect to WeightLog/ShowAllWeightLog return(RedirectToAction("ShowAllWeightLog", new { user_id = Session["Id"] })); } return(View()); }
public ActionResult AddWeightLog(WeightLogModel weightLog) { //check if the model state is valid or not if (ModelState.IsValid) { //save data in the database impl.SaveWeight(weightLog); //shows message to user if data is inserted ViewBag.Message = "Weight Inserted successfully"; //deletes the data from model ModelState.Clear(); } return(View()); }
//UpdateWeight() update the WeightLogs based on WeightId and WeightLogModel public bool UpdateWeight(int weight_id, WeightLogModel weight) { //open a connection to a database FitnesspointDatabase using (var context = new FitnesspointDatabaseEntities()) { //retrieving weightLog from the database WeightLog based on WeightId var weightLog = context.WeightLogs.FirstOrDefault(x => x.WeightId == weight_id); //replace the data in the database with WeightLogModel data based on WeightId if (weightLog != null) { weightLog.Weight = weight.Weight; weightLog.Updated_At = DateTime.UtcNow; weightLog.UserId = weight.UserId; } //save the changes to the database context.SaveChanges(); //returns true if data is updated return(true); } }
//SaveWeight() saves WeightLog in the database public WeightLog SaveWeight(WeightLogModel weight) { //open a connection to a database FitnesspointDatabase using (var context = new FitnesspointDatabaseEntities()) { //Creating WieghtLog object and assigning data using WeightLogModel class WeightLog weightLog = new WeightLog() { Weight = weight.Weight, Created_At = DateTime.UtcNow, Updated_At = DateTime.UtcNow, UserId = weight.UserId }; //add weightLog to the database WeightLog using model WeightLog context.WeightLogs.Add(weightLog); //save the changes to the database context.SaveChanges(); return(weightLog); } }