如何使用Asp.Net中的按钮来调用方法 [英] How to use buttons in Asp.Net to call a method
问题描述
我试图在View中创建一个按钮,使用该按钮的onClick事件在Model中调用一个方法,但是我不确定如何使它起作用.该按钮什么都不做.
I am trying to make a button in my View call a method in the Model using the button's onClick event, but I am not sure how to make this work. The button just doesn't do anything.
这是我的观点:
<html>
<body>
<div>
<p>Coins: @Model.Coins</p>
<button type="button" id="btnScore"
onclick="Btn_Click">Click me!</button>
</div>
</body>
</html>
这是我尝试在其中调用方法的模型:
This is the model I try to call the method in:
public class ClickerGame
{
public int Coins { get; set; }
public ClickerGame()
{
Coins = 0;
}
public void Btn_Click()
{
Coins++;
}
}
这是控制器:
public class ClickerController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
ClickerGame model = new ClickerGame();
return View(model);
}
}
这是我的Startup.cs
And this is my Startup.cs
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Clicker}/{action=Index}/{id?}");
});
}
}
我能够在视图中调用@ Model.Coins来获得硬币数量,但是当我尝试使用@ Model.Btn_Click时,出现了一条错误,提示该方法无法转换为委托对象. 我正在Visual Studio中使用Asp.Net Core 1.0.1.
I am able to get the amount of coins in calling @Model.Coins in the View, but when I try to use @Model.Btn_Click it gives me an error saying the method can't be converted to a delegate object. I am using Asp.Net Core 1.0.1 in visual studio.
我更改了控制器和视图,现在可以调用该方法,但是当它尝试增加硬币变量时,我得到了NullReference异常...
I changed the Controller and the View and it now is able to call the method, but when it tries to increase the coin variable I get a NullReference exception...
视图:
@model AspNetClicker.Models.ClickerGame
<script>
function IncrementCount()
{
location.href = "@Url.Action("IncrementCount")";
}
</script>
<html>
<body>
<div>
<p>Coins: @Model.Coins</p>
<button type="button" id="btnScore"
onclick="IncrementCount()">
Click me!
</button>
</div>
</body>
</html>
ClickerGame:
ClickerGame:
public class ClickerGame
{
public int Coins { get; set; }
public ClickerGame()
{
Coins = 0;
}
}
ClickerController:
ClickerController:
public class ClickerController : Controller
{
ClickerGame model;
public IActionResult Index()
{
model = new ClickerGame();
return View(model);
}
public void IncrementCount()
{
model.Coins++;
}
}
推荐答案
不可能像在Webforms中那样. 我将简要说明.请实施. 请按照以下步骤使其在MVC中运行:
It is not possible to do that like in webforms. I will explain briefly. Please implement it. Follow the following steps to make it work in MVC :
1.将您的查看代码更改为以下代码:
<html>
<body>
<div>
<p>Coins: @Model.Coins</p>
<button type="button" id="btnScore"
onclick="IncrementCount()">Click me!</button>
</div>
</body>
<script>
function IncrementCount() {
$.ajax({
type: "POST",
url: "/Clicker/IncrementCount",
async: true,
success: function (msg) {
ServiceSucceeded(msg);
},
error: function () {
return "error";
}
});
}
</script>
</html>
2.在您的控制器中添加操作方法IncrementCount
并在此处增加计数.
2. In your controller add an action method IncrementCount
and increment the count here.
public class ClickerController : Controller
{
[HttpPost]
public void IncrementCount()
{
//
}
}
这篇关于如何使用Asp.Net中的按钮来调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!