Skip to content

Instantly share code, notes, and snippets.

@tklives
Forked from Drosty/Controller
Created June 6, 2013 18:39
Show Gist options
  • Save tklives/5723855 to your computer and use it in GitHub Desktop.
Save tklives/5723855 to your computer and use it in GitHub Desktop.
using System;
using System.Web.Mvc;
namespace AjaxTest.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult TestPost(PostData postedData)
{
var obj = new DummyData {Here = "Is me", What = "Is that", Echo = postedData.PostedGuid};
return Json(obj);
}
}
public class PostData
{
public Guid? PostedGuid { get; set; }
}
public class DummyData
{
public string What { get; set; }
public string Here { get; set; }
public Guid? Echo { get; set; }
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title - My ASP.NET MVC Application</title>
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<meta name="viewport" content="width=device-width" />
<script src="~/Scripts/jquery-1.8.2.js"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
</head>
<body>
<div id="body">
<section>
@RenderBody()
</section>
</div>
</body>
<script type="text/javascript">
function CallMe(input) {
alert(input);
}
</script>
</html>
@model AjaxTest.Controllers.PostData
@{
ViewBag.Title = "Home Page";
}
<div id="result"></div>
@using (Ajax.BeginForm("TestPost", "Home", new AjaxOptions { OnComplete = "CallMe" }))
{
@Html.EditorFor(x => x.PostedGuid)
@Html.ValidationMessageFor(x => x.PostedGuid)
<input type="submit" value="OK" />
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment