Brad Mathews February 2016

Ajax.BeginForm not submitting form

I am doing an MVC tutorial and have hit a problem.

I changed a working view from using Html.BeginForm to Ajax.BeginForm. But the form is not submitting.

Per my research I verified that ClientValidationEnabled and UnobtrusiveJavaScriptEnabled are both set to true in web.config. I do have the most recent version of jquery.unobtrusive-ajax,js and jquery refrenced. But still no joy.

I firebugged jquery.unobtrusive-ajax to see what it was doing. It is setting itself up on page load. But no functions are called when I click the submit button.

Here is a snippet from View Source to show how the form tag is begin set up:

<div id="formSection">
  <h3>Having trouble? Send me a message</h3>
  <form action="/Home/Contact" data-ajax="true" data-ajax-method="post" data-ajax-mode="replace" data-ajax-update="#formSection" id="form0" method="post">
  </form>    
  <textarea name="message"></textarea>
  <input type="submit" value="send" />
</div>

Answers


Brad Mathews February 2016

My error became apparent while asking the question. I was cleaning up the bad formatting of the HTML when I saw the end form tag was before the submit button!

I had an error in my Razor code.

@Using (Ajax.BeginForm("Contact", "Home", New AjaxOptions With {.HttpMethod = "post", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "formSection"}))
End Using
<textarea name="message"></textarea>
<input type="submit" value="send" />

Putting the End Using right after the Using closed the block of course, so the input element never got into the form.

Correct syntax:

@Using (Ajax.BeginForm("Contact", "Home", New AjaxOptions With {.HttpMethod = "post", .InsertionMode = InsertionMode.Replace, .UpdateTargetId = "formSection"}))
  @<textarea name="message"></textarea>
  @<input type="submit" value="send" />
End Using

It would have been nice if the generated code was formatted well, I would have seen the end form tag location much sooner.

Hopefully I help avoid a few head slaps.

Post Status

Asked in February 2016
Viewed 1,457 times
Voted 11
Answered 1 times

Search




Leave an answer