Skip to content

Instantly share code, notes, and snippets.

@rofr
Created December 21, 2016 11:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rofr/9ff800645292d93629920921fc8e9dc2 to your computer and use it in GitHub Desktop.
Save rofr/9ff800645292d93629920921fc8e9dc2 to your computer and use it in GitHub Desktop.
MVC Razor view with jQuery AJAX calling MVC controller method
public class HomeController : Controller
{
public ActionResult Math()
{
return View("Math");
}
public ActionResult Add(int a, int b)
{
int sum = a + b;
return Content(sum.ToString());
}
}
<h2>title</h2>
<input id="a"/> + <input id="b"/> = <span id="result"></span>
<br/>
<button id="addButton">Add</button>
@section Scripts {
<script>
$(function() {
$("#addButton")
.click(function () {
var a = $("#a").val();
var b = $("#b").val();
$.get("/Home/Add",
{ "a": a, "b": b },
function (data) {
$("#result").text(data);
}
);
});
})
</script>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment