Ajax.BeginForm doesn't post properly

2019-02-21 04:11发布

问题:

I have a simple requirement I seem unable to meet: I have a product page. products have vendors and the vendor input is a text field with an autocomplete. if the user enters a vendor that doesn't exist in the database, I need to add it. to add it I have a DIV on the page which I .load() with a call to my /Vendor/Create controller method. the view for that method uses:

@using (Ajax.BeginForm("Create", "Vendor", new AjaxOptions {
     UpdateTargetId = ViewBag.TargetId,
     HttpMethod = "Post", 
     OnSuccess="VendorCreated", 
     OnComplete="alert('success')"
})) {

which should post my form via ajax, calling Javascript when done. The problem I'm having is that upon submission my whole page gets refreshed such that I end up with /Vendor/Create when I started with /Product/Create. additionally, neither the OnSuccess nor OnComplete get called.

what am I doing wrong here?

回答1:

Make sure you have included the following scripts to your page:

jquery-1.5.1.js
jquery.unobtrusive-ajax.js

and that unobtrusive javascript is enabled in web.config:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />