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";
}
Index2
function PostFailure(){
alert("Failure");
}
function PostSuccess(){
alert("Success");
}
function PostOnComplete() {
alert("Complete");
}
Page Rendered: @DateTime.Now.ToLongTimeString()
@using (Ajax.BeginForm("TestAjax", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "textEntered", OnFailure = "PostFailure", OnSuccess = "PostSuccess", OnComplete = "PostOnComplete" }))
{
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.LabelFor(m => m.Password)
@Html.TextBoxFor(m => m.Password)
@Html.LabelFor(m => m.EmailAddress)
@Html.TextBoxFor(m => m.EmailAddress)
}