am new to ASP.net. Am trying to have a simple page view that has a button and when clicked, this would call a method that is in a controller, to then do more code and computation with c#.
I read posts and tried this but the method does not seem to be called, the debugger never stops in the Controler ClickButton1 method.
My VIEW code
<div>
<input id="Button1" type="button" value="Button 1" onclick="ClickButton1()" />
<input id="Button2" type="button" value="Button 2" onclick="location.href='@Url.Action("Options", "Home")'" />
</div>
<div>
<canvas id="mapCanvas" width="500" height="150" style="border:1px solid #000000;"></canvas>
</div>
<script>
function ClickButton1() {
$.ajax({
type: "POST",
url: '@Url.Action("ClickButton1", "Definition")',
async: true,
success: function (msg) {
ServiceSucceeded(msg);
},
error: function () {
return "error";
}
});
}
</script>
My CONTROLLER
namespace Traveler.Controllers
{
public class DefinitionController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
return View();
}
// Action click on Button 1
public void ClickButton1()
{
Console.WriteLine("Button 1 Clicked");
}
}
}
ClickButton1marked asHttpPost? Nope. But your JavaScript is trying to POST to it. Why are you doingreturn "error";instead of getting the error message? Did you bother looking at the browser's developer tools at the network traffic?[HttpPost]attribute toClickButton1. You would probably see an error message to this effect if you used your browser's developer tools to watch the network traffic. I suggest you learn how to do this, because seeing exactly the response from the server is something you need to know how to do.