BeginRouteForm not working with API routes?

I have a form that I am trying to create using relatively simple code:

@using (Ajax.BeginRouteForm("DefaultApi", new { controller = "persons" }, new AjaxOptions {HttpMethod = "post", OnSuccess = "someSuccess"}))} { @Html.TextBoxFor(m => m.Name) } 

I have a PersonController, which is an ApiController, and I have the following route in WebApiConfig:

 config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); 

But calling BeginRouteForm returns no action. The resulting HTML:

 <form action="" data-ajax="true" data-ajax-method="post" data-ajax-success="someSuccess" id="form0" method="post"> </form> 

As you can see, the action is empty. He does not complain that he cannot find a route (which he could if he could not), so what is happening? I even tried to provide an empty identifier in the routeValues ​​object, but that would not work either (although this is not necessary, since id is optional).

What am I missing here? Can't you use BeginRouteForm with routes added via MapHttpRoute? This behavior is the same with Html.BeginRouteForm and Ajax.BeginRouteForm (as they should be, they have the same base). Obviously, I expected that in this case the action would return to / api / people.

+4
source share
1 answer

Having MVC 5 and Web API 2.2, the following inscription in the view worked for me:

 @using (Ajax.BeginRouteForm("DefaultApi", new { controller = "User", HttpRoute = "true" }, new AjaxOptions { HttpMethod = "POST", OnFailure = "onFailure", OnSuccess = "onSuccess" })) { } 
0
source

Source: https://habr.com/ru/post/1481473/


All Articles