Html.BeginForm() going to the wrong action on my controller

Viewed 248

I have an HTML form built using Html.BeginForm(). I specify my controller name and action name, however it does not go to the correct action.

Here is the relevant line from my view:

@using (Html.BeginForm("AdminPanelResult", "AdminPanel", FormMethod.Post))

In my controller I have two methods. No matter what I do it always goes to AdminPanel() and not AdminPanelResult().

Here is what AdminPanel looks like:

public ActionResult AdminPanel() {...}

And this is AdminPanelResult():

[System.Web.Http.HttpPost]
public ActionResult AdminPanelResult(ContentModel model) {...}

My question, how do I get Html.BeginForm to actually use the correct action?

EDIT: As requested here is the rest of my view's form:

    <legend>Admin Panel Access</legend>
@using (Html.BeginForm("AdminPanelResult", "AdminPanel", FormMethod.Post))

{
    <fieldset>
        <div id="AdminPanelResult"></div>
        <ol>
            <label for="selOrgName">Choose an Organization Name:</label>
            <select name="selOrgName" id="selOrgName">
                <option id="0">Select an Org Name</option>

                @foreach (var r in data.glOrgName)
                {
                    <option id="@r.OrgKey" value="@r.OrgName">@r.OrgName</option>
                }
            </select>
            <label for="apiDrop">Choose the type of Data:</label>
            @Html.DropDownList("ApiId", (MultiSelectList)ViewBag.Categories, new { multiple = "multiple" })


            <li>
                @Html.Label("...")
                @Html.TextBox("..")
            </li>
    <!--Several more identical elements removed for brevity-->
        </ol>

        <p>
            <input type="submit" value="Submit" />
        </p>

    </fieldset>
}
1 Answers

Assuming that your controller name is "AdminPanel" if you need to submit form to AdminPanelResult action use this

@using (Html.BeginForm("AdminPanelResult", "AdminPanel", FormMethod.Post))

or if you need to submit form to AdminPanel action use this

@using (Html.BeginForm("AdminPanel", "AdminPanel", FormMethod.Post))

and since I can' t see your view you have to use submit button to submit form, not the ancor tag. Ancor tag always generate a get method and doesn't follow form action.

if it still not working you have to fix routing. I can't see your controller, but this will certainly work

[HttpPost("~/AdminPanel/AdminPanelResult")]
public ActionResult AdminPanelResult(ContentModel model) {...}
Related