public ActionResult SaveTextInput(TextInputDto input) { if (!ModelState.IsValid) //server side validation { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = "Failed to save new User Input due to invalid data"; if (input.Id == 0) //depending on user action at the time { return(RedirectToAction("AddUserInput", new { type = UserInputType.Text })); } return(RedirectToAction("UpdateUserInput", new { type = UserInputType.Text, id = input.Id })); } int entityId; //new id of entity if not existing try { entityId = _portfolioService.ModifyTextInput(UserId, input, input.Id > 0 ? EntityModification.Update : EntityModification.Create).Id; } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to save new User Input, error: {exception.Message}"; if (input.Id == 0) //depending on user action at the time { return(RedirectToAction("AddUserInput", new { type = UserInputType.Text })); } return(RedirectToAction("UpdateUserInput", new { type = UserInputType.Text, id = input.Id })); } TempData["MessageType"] = WebMessageType.Success; TempData["Message"] = "Successfully saved new User Input"; return(RedirectToAction("ShowUserInput", new { id = entityId, UserInputType.Text })); }
/// <summary> /// Return view to update a user input /// </summary> /// <param name="type"></param> /// <param name="id"></param> /// <returns></returns> public ActionResult UpdateUserInput(UserInputType type, int id) { IUserInput input; switch (type) { case UserInputType.Text: input = _portfolioService.GetTextInput(UserId, id); break; case UserInputType.Selection: input = _portfolioService.GetSelectionInput(UserId, id); break; case UserInputType.ScriptedSelection: input = _portfolioService.GetScriptedSelectionInput(UserId, id); break; default: //need a default input = new TextInputDto(); break; } //input.ServiceOptionId = id; UserInputModel model = new UserInputModel { InputType = type, UserInput = input, Action = "Update" }; return(View("EditUserInput", model)); }
/// <summary> /// Returns View to add form of corresponding type /// </summary> /// <param name="type"></param> /// <returns></returns> public ActionResult AddUserInput(UserInputType type) { UserInputModel model = new UserInputModel(); IUserInput input; switch (type) { case UserInputType.Text: input = new TextInputDto(); break; case UserInputType.ScriptedSelection: input = new ScriptedSelectionInputDto(); break; case UserInputType.Selection: input = new SelectionInputDto { Delimiter = "," }; //set the default to comma break; default: //need a default input = null; //null is ok, razor will handle break; } model.InputType = type; model.UserInput = input; model.Action = "Add"; return(View("EditUserInput", model)); }
/// <summary> /// Show details of a user input /// </summary> /// <param name="type">input type</param> /// <param name="id">id of input</param> /// <returns></returns> public ActionResult ShowUserInput(UserInputType type = UserInputType.Text, int id = 0) { var model = new UserInputModel { InputType = type }; IUserInput input; if (id > 0) { try { switch (type) { case UserInputType.Text: input = _portfolioService.GetTextInput(UserId, id); break; case UserInputType.ScriptedSelection: input = _portfolioService.GetScriptedSelectionInput(UserId, id); break; case UserInputType.Selection: input = _portfolioService.GetSelectionInput(UserId, id); break; default: //need a default input = new TextInputDto(); break; } } catch (Exception exception) { TempData["MessageType"] = WebMessageType.Failure; TempData["Message"] = $"Failed to retreive user input, error: {exception.Message}"; input = new TextInputDto(); //some default where id = 0 } } else { input = new TextInputDto(); //some default where id = 0 } //input.ServiceOptionId = id; model.UserInput = input; return(View("ShowUserInput", model)); }