MVC 3 – Ajax.BeginForm做了一个完整的回发

在下面的代码中,我使用Ajax.BeginForm将数据asynchronous发布到操作。 该操作被调用,但结果显示在新的网页上。 我看了很多例子。 这似乎并不困难。 我已经使这个例子对于概念certificate(poc)非常简单,但是我仍然看到显示一个新的页面。

调节器

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

视图

 @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> 

你能检查_Layout.cshtml并确保ajax脚本被引用? 我不认为这是默认的:

 <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script> 

还要记住,你需要在webconfig中

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