Multiple posts when submitting a form in a block

Vote:
 

I have created a completely standard block that submits a form.  When the same block is added to the page twice and one of the forms are submitted, the block controller's Post function also gets called twice?

In the block's view:

@using (Html.BeginForm())
{
    

And in the block's controller:

[HttpPost]
public ActionResult Index(myVM model, string myValue)
{
    ...
}

Since it is the same block, the viewmodel is not unique so I have no way of filtering out the 2nd Post.

Has anyone else had to deal with this?

Thanks

Simon

#149613
Jun 06, 2016 14:46
Vote:
 

Would be interested to hear if anyone has an actual solution to this but I have settled on filtering out the extra posts by settings a flag in TempData using these extensions:

    public static class ControllerExtensions
    {
        private  static string GetRunFlagKey(ContentData block, string key)
        {
            return "RunFlag" + (block as IContent).ContentLink + key;
        }

        public static bool GetRunFlag(this Controller controller, ContentData currentContent, string key)
        {
            var b = controller.TempData[GetRunFlagKey(currentContent, key)] as bool?;
            return b.HasValue;
        }


        public static void SetRunFlag(this Controller controller, ContentData currentContent, string key)
        {
            controller.TempData.Add(GetRunFlagKey(currentContent, key), true);
        }
    }

In the controller's Post function I simply have to check if the post has been called already:

        [HttpPost]
        public ActionResult Index(myModel model, string myValue)
        {
            if (!this.GetRunFlag(model.CurrentBlock, myValue))
            {
                this.SetRunFlag(model.CurrentBlock, myValue);
                ....
#149769
Jun 07, 2016 14:12
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.