Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Form generated by Html.BeginForm doesn't match action name given.

$
0
0

I have a controller like this:

[HttpGet("[controller]/CreateChild/{parentId}")]
[HttpGet("[controller]/ChangeChild/{childId}")]
public IActionResult CreateChild(int? childId, int? parentId?)
{
    // Only take 1 id as a parameter, route determines the type for the id passed in
    // If we have a parent, create a child object based on the parent,
    // otherwise if we have a child, load the existing child and populate fields based
    // on it.
}


[HttpPost]
public IActionResult CreateChild(ChildViewModel viewModel)
{

}

And a form in my View like this:

@using (Html.BeginForm("CreateChild", "Child", FormMethod.Post))
{

}

But when I load the form in a browser after loading an existing Child via "/Child/ChangeChild/5" , the <form> element's action points to "/Child/ChangeChild/5" as well instead of the "CreateChild" action I specified in the BeginForm method, which throws a 404 since I don't have a ChangeChild action defined for POST.

Why isn't the actionName defined in BeginForm being used? Is it related to my routing? I'm running an ASP.NET MVC Core app with my routing configured like this:

app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>