35

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>
tereško
  • 58,060
  • 25
  • 98
  • 150
Mike Barlow - BarDev
  • 11,087
  • 17
  • 62
  • 83

2 Answers2

67

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>
Jeff Ogata
  • 56,645
  • 19
  • 114
  • 127
  • Thanks for the solution. I've been fighting this for over a day. The reference was not in the layout page, so I added it to the View. It seems to work now. Also, can you tell me what scripts I do not need? Thanks again – Mike Barlow - BarDev Feb 21 '11 at 00:25
  • Well, I removed all of the scripts except for jquery.unobtrusive-ajax.js and everything still seems to be working. – Mike Barlow - BarDev Feb 21 '11 at 00:27
  • @BarDev, for ajax, I think this is the only script you will need. If you end up using other functionality (like validation), then you'll need references to those scripts at that time. – Jeff Ogata Feb 21 '11 at 00:38
  • 1
    This answer is also true for MVC4, as I just experienced! – Marcel Mar 15 '13 at 12:10
  • It worked, but why? Does it ignore "@section Scripts" within a partial? – Arman Bimatov Nov 02 '13 at 23:52
15

Also remember that you need this in the webconfig

  <appSettings>
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
Mike Barlow - BarDev
  • 11,087
  • 17
  • 62
  • 83