Created
December 21, 2016 11:37
-
-
Save rofr/9ff800645292d93629920921fc8e9dc2 to your computer and use it in GitHub Desktop.
MVC Razor view with jQuery AJAX calling MVC controller method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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