'ASP.NET MVC: Method for HttpPost is not executed

In an ASP.NET MVC application, I have a file MessageController.cs where I define EditMessage for access via HttpGet and HttpPost. Usually the user first accesses it via HttpGet, then a form pops up where he can edit the message, and then he clicks on the Save button, by which HttpPost will be invoked.

My problem is that HttpPost is not invoked. Instead, an error message is displayed. I have analogous code for modifying other parts of the database and with that analogous code, HttpPost works. My question is why it does not work here and what I can do to make it work.

    /// <summary>
    /// used by admin only
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [CustomAuthorization(new[] { GlobalStaticFunc.SecurityOptions.isSUser, GlobalStaticFunc.SecurityOptions.isAdmin })]
    [HttpGet]
    public async Task<ActionResult> EditMessage(int id)
    {
        if (PopupController.AnyPopupsInline(User))
        {
            return RedirectToAction("index", "popup");
        }

        if (id > 0)
        {
            BLogic_Entity dbContext = new VMIEntityCreator(true);
            var msg = dbContext.GetDbSet<MSG_MESSAGES>().Where(x => x.id == id).FirstOrDefault();
            if (msg != null) return View(msg);
        }
        else if (id == -1)
        {
            return View(new MSG_MESSAGES() { id = -1 });
        }
        return View("Messages");
    }
    [CustomAuthorization(new[] { GlobalStaticFunc.SecurityOptions.isCarrier, GlobalStaticFunc.SecurityOptions.isAdmin, GlobalStaticFunc.SecurityOptions.isSUser })]
    [HttpPost]
    //        [ValidateAntiForgeryToken]
    public async Task<ActionResult> EditMessage(MSG_MESSAGES model)
    {
        if (PopupController.AnyPopupsInline(User))
        {
            return RedirectToAction("index", "popup");
        }

        if (!App_Tools.RightsHandler.IdentityWatcher.CheckUserRights(User.Identity, GlobalStaticFunc.SecurityOptions.isAdmin) && App_Tools.RightsHandler.IdentityWatcher.CheckUserRights(User.Identity, GlobalStaticFunc.SecurityOptions.isEndUser))
        {
            return RedirectToAction("Messages", "Message");
        }

        bool isOk = false;
        if (model != null)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }
            if (model.id > 0)
            {
                using (TED_BLOGIC.Abstractions.BLogic_Entity usr = new VMIEntityCreator(true))
                {
                    isOk = await usr.UpdateSecurely(usr.GetDbSet<MSG_MESSAGES>().Where(x => x.id == model.id).FirstOrDefault(), model, ModelState);
                }
            }
        }
        return View(model);
    }

The code of EditMessage.cshtml:

    @model TED_BLOGIC.DataBase.DB_MODEL.MSG_MESSAGES

    @{
        if (Model != null && Model.id > 0)
        {
            ViewBag.Title = "Message bearbeiten";
        }
        else
        {
            ViewBag.Title = "Neue Message anlegen";
        }
        ViewBag.Ico = "fa-plus";
        Layout = "~/Views/Shared/_standardBoxView.cshtml";
    }
    @using (Html.BeginForm("EditMessage", "Message", new { id = Model.id }, FormMethod.Post, new { data_goback = true }))
    {
        @Html.AntiForgeryToken()
        <div class="panel-body">
            <div class="row">
                <div class="col-md-12 table-responsive">

                    @Html.ValidationSummary(false, "", new { @class = "text-danger" })
                    @Html.EditorForModel("Add/MGV")
                    <div class="section row mb10">
                        <div class="form-group">
                            <div class="col-md-offset-2 col-lg-3 col-md-4 col-sm-5">
                                <input type="submit" value="Save" class="btn btn-default" onclick=";" /> @*mCust.postToSite(@Url.Action("User", "Admin"));mCust.sendMeBack()*@
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    }

    <script src="~/Scripts/Core/PostbackHandling/[email protected]"></script>

    <script>
        $(document).on("EverythingIsReady", function () {
            document.title = 'Cloud - @ViewBag.Title';
        })
    </script>


Solution 1:[1]

<input type="submit" value="Save" class="btn btn-default" onclick=";" />

Could you please try removing onclick attribute of the button?

Solution 2:[2]

please check by commenting CustomAuthorization attribute above that post method, the post-event will be fired. I tested your code.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Nagaraj Raveendran
Solution 2 sachind