I know this is an old post, I hope my experience will help someone:
I have two submit buttons and a general method that is called to process the form submission from both of them - conditional statements check which button submits the form.
On a button that requires user authentication, it returned HttpUnauthorizedResult() to return the user to the login page. After entering the system, the user will be sent back to the form with the field values ββfilled in, since they are stored in the TempData record, but the user still needs to click the button again to send the data.
So, I added a boolean variable called TempData called AutoSave , which will be set right before HttpUnauthorizedResult() returns. I test it before loading the form again, and if AutoSave == true , I just redirect the method that processes the view. It works like a charm.
Here is an example code:
private ActionResult ProcessOnlineApplication(OnlineApplicationViewModel application){ //if not submit make sure it save. if (application.SubmitAction == "Save"))) { if(!User.Identity.IsAuthenticated) { //Keep a copy of the application until logged in TempData["PendingOnlineApplication"] = application; TempData["AutoSave"] = true; return new HttpUnauthorizedResult(); } else{ //Everything goes here } } } public ActionResult OnlineApplicationForm(){ var viewModel = TempData["PendingOnlineApplication"] as OnlineApplicationViewModel; if (TempData.ContainsKey("AutoSave") && Convert.ToBoolean(TempData["AutoSave"]) && viewModel != null) { TempData["AutoSave"] = false; return ProcessOnlineApplication(viewModel); } if (viewModel == null) { viewModel = CreateModel(); } return GetOnlineApplicationAction(viewModel); }
source share