简体   繁体   中英

Ajax.BeginForm Bug in MVC2?? Ajax for Posting to Wrong Controller?

I am using an ajax form trying to post back the the CustomerController using the Create method. Here is the code

<% using (Ajax.BeginForm("Create", "Customer", new AjaxOptions { LoadingElementId = "saving"}, new { @class = "block_content form" }))
        {%>...

When my html form renders the form looks like this

<form onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, loadingElementId: 'saving', onComplete: Function.createDelegate(this, $j('#accoutcreate').dialog('close')) });" onclick="Sys.Mvc.AsyncForm.handleClick(this, new Sys.UI.DomEvent(event));" method="post" class="block_content form" action="/Job/Create?Length=3"> ...

As you can see the form is actually being posted to /Job/Create instead of /Customer/Create

I am not sure why this is happening. Any ideas?

I guess, framework have taken not appropriate method. Maybe this:

public static MvcForm BeginForm(this AjaxHelper ajaxHelper,
string actionName, object routeValues, AjaxOptions ajaxOptions,
object htmlAttributes);

And you've provided:

"Create" - actionName,   "Customer" - routeValues,   new AjaxOptions(..) - ajaxOptions,   new { @class = "block_content form" }
- htmlAttributes.

Note, that routeValues has object type and "Customer" - string and everything is Ok for C# compiler but not for you.

Try to write:

(Ajax.BeginForm("Create", "Customer", null,
new AjaxOptions { LoadingElementId = "saving"}
new { @class = "block_content form" }))

Is it possible you have a route setup in the Global.asax that maps "\\Job" requests to the "CustomerController"? If so, the routing engine in MVC would return "\\Job" as the URL in order to keep URLs consistent.

The route would look something like this:

        routes.MapRoute("Name", "Job/Create", new { controller = "Customer", action = "Create" });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM