title: ASP.NET MVC Reload page form Re-submission with alert
body: <p>Attention, this is a little long and complex problem here, so I suggest to read and help me with this only if you have good amount of time.</p>
  
  <p>To begin with, I am working on a web project where I am at administration section where I can ban, unban or promote a user. So, this is a same view with 3 different models included as shown:</p>
  
  <p>My model:</p>
  
  <pre><code>public class dynamicAdminModel : NoResubmitAbstract
  {
      public UserSuspension userSus { get; set; }
      public UserUnban userUnban { get; set; }
      public PromoteUser promoteUser { get; set; }
  }
  </code></pre>
  
  <p>Now, let say that I am promoting a user. I submit and I get that alert message "Success! User Billy has been promoted to Moderator.". But now when I refresh, I get that alert message again.</p>
  
  <p>So, basically, the form is resubmitting and I can't have that. Here is more of my code:</p>
  
  <p>Controller:</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public virtual ActionResult DevelopmentBoard(dynamicAdminModel user)
  {
      if (ModelState.IsValid)
      {
          string str = getDevString(user);
          switch (str)
          {
              case "Banning":
                  userBanning(user.userSus);
                  break;
              case "Unbanning":
                  unbanUser(user.userUnban.UserName);
                  break;
              case "PromoteUser":
                  promoteUser(user.promoteUser.UserName, user.promoteUser.Hierarchy);
                  break;
          }
      }
      return View(user);
  }
  </code></pre>
  
  <p>In promoteUser function: (I used viewbags just to test before I put actual functions)</p>
  
  <pre><code>private void promoteUser(string userName, string Hierarchy)
  {
      ViewBag.SuccessCode = "UserPromoted";
      ViewBag.User = userName;
      ViewBag.Hierarchy = Hierarchy;
  }
  </code></pre>
  
  <p>In cshtml: (I closed first 2 tabs and left the 3rd one open)</p>
  
  <p><a href="https://i.stack.imgur.com/IpoYJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IpoYJ.png" alt="enter image description here"></a></p>
  
  <p>In Javascript Code:</p>
  
  <pre><code>switch ("@ViewBag.SuccessCode") {
      case "UserBanned":
          alert("User has been banned successfully!");
          break;
      case "UserUnbanned":
          alert("User has been successfully unbanned!");
          break;
      case "UserPromoted":
          alert("Success!\nUser @ViewBag.User has been promoted to @ViewBag.Hierarchy.");
          break;
  }
  </code></pre>
  
  <hr>
  
  <p>Overall, everything works the way suppose to, it's just that form re-submission. I have been researching for awhile and found the only solution that can resolve more simple cases which doesn't help. Just this one I'm dealing with is complicated.</p>
  
  <p>There are 2 things I tried. One, I tried to use Abstract class with virtual and added additional static functions. Two, I tried using other ActionResult.</p>
  
  <hr>
  
  <p>Let me elaborate on the second approach. Basically, after I submit, I go to that other action result, and that other action result automatically takes me back. BUT, the problem is that, the alert doesn't popup if I press Submit. Thankfully, the form doesn't resubmit.</p>
  
  <p>Basically this code in Controller:</p>
  
  <pre><code>public ActionResult sbmtPage()
  {
      return RedirectToAction("DevelopmentBoard", "Administrator");
  }
  </code></pre>
  
  <p>And I use in ActionResult DevelopmentBoard "return RedirectToAction("sbmtPage", "Administrator")".</p>
  
  <hr>
  
  <p>The first approach is a little bit more complicated. What it is is that I created an additional static class inside the controller with 2 functions; IsResubmit and PreventResubmit. </p>
  
  <p>Code for it:</p>
  
  <pre><code>public static class ControllerExtensions
  {
      [NonAction]
      public static bool IsResubmit(this System.Web.Mvc.ControllerBase controller, NoResubmitAbstract vModel)
      {
          try
          {
              return (Guid)controller.TempData["PreventResubmit"] != vModel.PreventResubmit;
          }
          catch (Exception)
          {
              return false;
          }
      }
  
      [NonAction]
      public static void PreventResubmit(this System.Web.Mvc.ControllerBase controller, params NoResubmitAbstract[] vModels)
      {
          var preventResubmitGuid = Guid.NewGuid();
          controller.TempData["PreventResubmit"] = preventResubmitGuid;
          foreach (var vm in vModels)
          {
              vm.SetPreventResubmit(preventResubmitGuid);
          }
      }
  }
  </code></pre>
  
  <p>And as I said about virtual action result I mentioned earlier where you see "public virtual ActionResult DevelopmentBoard". AND that abstract class in "NoResubmitAbstract" in dynamicAdminModel.</p>
  
  <p>Code: (Abstract inside the model)</p>
  
  <pre><code>public abstract class NoResubmitAbstract
  {
      public Guid PreventResubmit { get; set; }
  
      public void SetPreventResubmit(Guid prs)
      {
          PreventResubmit = prs;
      }
  }
  </code></pre>
  
  <p>This works when I use "this.PreventResubmit(user);" with/without "this.IsResubmit(user)" inside the ActionView. However, it only works the first time, no more than once, which kind of sucks. Even though if I remove PreventResubmit and used this instead:</p>
  
  <pre><code>if(this.IsResubmit(user)){
      if(ModelState.IsValid){
          Code....
      }
  }
  </code></pre>
  
  <p>No luck unfortunately. But hey! No form re-submission whenever I refresh!</p>
  
  <hr>
  
  <p>SO, Overall, what do you guys think? Will I need help with the first approach or the second? I think the second one makes more sense. If there is another approach, please tell me. If you have any questions or if you need me to include more code, let me know, thanks! I'll be back in 10 hours.</p>