您可以選擇的jQuery + Ajax來EXCUTE後面的代碼,例如:
剃刀頁(Contact.cshtml)
<input type="button" value="Button1" id="button1" />
<input type="button" value="Button2" id="button2" />
JavaScript代碼
$(function() {
$("#button1").click(function() {
$.post("/Sample/Action1", function (result) {
alert("Click Button1,Action1 Success!");
});
});
$("#button2").click(function() {
$.post("/Sample/Action2", function (result) {
alert("Click Button2,Action2 Success!");
});
});
});
代碼(SampleController.cs)背後
[HttpPost]
public JsonResult Action1()
{
//action 1 ...
return Json(new { Success = true });
}
[HttpPost]
public JsonResult Action2()
{
//action 2 ...
return Json(new { Success = true });
}
在此之前,你應該確保jQuery庫包含在您的網頁。
https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio您可以檢查Razor Pages框架。沒有WebForms for ASP.NET Core。 –