Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 - Ajax.BeginForm does a full post back

In the following code, I'm using Ajax.BeginForm to post data to the action asynchronously. The action gets called but the results are displayed to a new web page. I'v looked at a ton of example. This doesn't seem difficult. I've made the example extremely simple for a proof of concept (poc), but I'm still seeing a new page displayed.

Controller

  [HttpPost]     [OutputCache(Location = OutputCacheLocation.None, NoStore = true)]     public string TestAjax(UserViewModel viewModel)     {          return viewModel.UserName;     } 

View

@model BasicMvc3Example2.Models.UserViewModel  @{     ViewBag.Title = "Index2";     Layout = null;//"~/Views/Shared/_Layout.cshtml"; }        <script src="/BasicMvc3Example2/Scripts/jquery-1.4.4.js" type="text/javascript"></script>     <script src="/BasicMvc3Example2/Scripts/jquery-ui.js" type="text/javascript"></script>     <script src="/BasicMvc3Example2/Scripts/jquery.validate.js" type="text/javascript"></script>     <script src="/BasicMvc3Example2/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>     <h2>Index2</h2>      <script type="text/javascript">         function PostFailure(){             alert("Failure");         }          function PostSuccess(){             alert("Success");         }          function PostOnComplete() {             alert("Complete");         }     </script>      Page Rendered: @DateTime.Now.ToLongTimeString()     @using (Ajax.BeginForm("TestAjax", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "textEntered", OnFailure = "PostFailure", OnSuccess = "PostSuccess", OnComplete = "PostOnComplete" }))     {         <div>            @Html.LabelFor(m => m.UserName)            @Html.TextBoxFor(m => m.UserName)         </div>          <div>            @Html.LabelFor(m => m.Password)            @Html.TextBoxFor(m => m.Password)         </div>          <div>            @Html.LabelFor(m => m.EmailAddress)            @Html.TextBoxFor(m => m.EmailAddress)         </div>          <input type="submit" id="submitForm" value="Submit Form" />     }      <div id="textEntered">d</div> 
like image 496
Mike Barlow - BarDev Avatar asked Feb 21 '11 00:02

Mike Barlow - BarDev


People also ask

What is difference between HTML BeginForm and ajax BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.

What is ajax BeginForm in MVC?

Ajax. BeginForm is the extension method of the ASP.NET MVC Ajax helper class, which is used to submit form data to the server without whole page postback. To work Ajax. BeginForm functionality properly, we need to add the reference of jquery.

What is @using HTML BeginForm ()) in MVC?

BeginForm(HtmlHelper) Writes an opening <form> tag to the response. The form uses the POST method, and the request is processed by the action method for the view. BeginForm(HtmlHelper, String, String, Object, FormMethod, Object)


2 Answers

Can you check _Layout.cshtml and make sure the ajax script is referenced? I don't think it is by default:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 
like image 154
Jeff Ogata Avatar answered Sep 17 '22 18:09

Jeff Ogata


Also remember that you need this in the webconfig

  <appSettings>     <add key="ClientValidationEnabled" value="true" />     <add key="UnobtrusiveJavaScriptEnabled" value="true" />   </appSettings> 
like image 24
Mike Barlow - BarDev Avatar answered Sep 19 '22 18:09

Mike Barlow - BarDev