[英]How do I call a function in my controller from a button in my .cshtml-page?
I'm trying to create a dashboard/graphical interface for managing purposes.我正在尝试创建用于管理目的的仪表板/图形界面。 How can I create buttons that call certain functions from a controller?如何创建从 controller 调用某些功能的按钮? It's in ASP.NET CORE 3, using the MVC-pattern.它位于 ASP.NET CORE 3 中,使用 MVC 模式。
What I want to do in the application is executing c# code by calling a method from my Index.cshtml page and passing parameters.我想在应用程序中做的是通过从我的 Index.cshtml 页面调用方法并传递参数来执行 c# 代码。
I've tried multiple solutions, namely those that state that the view and controller are synced with the controller function looking for the "equally named" view but it just doesn't work. I've tried multiple solutions, namely those that state that the view and controller are synced with the controller function looking for the "equally named" view but it just doesn't work.
Edit: Found the solution: I needed to specify both the controller and ActionResult.编辑:找到解决方案:我需要同时指定 controller 和 ActionResult。
<form action="Home/Change" method="post">
<input type="text" name="DoorID" placeholder="Guid.." />
<br />
<input type="submit" value="Post" />
</form>
I was apparently not smart enough to read the countless tutorials.我显然不够聪明,无法阅读无数的教程。 Thanks for the help!谢谢您的帮助!
If you want to send a piece of data after the button click to the server, you should create a form in your view and a post action in your controller.如果您想在单击按钮后向服务器发送一条数据,您应该在您的视图中创建一个表单并在您的 controller 中创建一个发布操作。 However, if you don't want to post any data and you want to simply do something after button click, creat a get action in your controller and navigate to that action path using button click event.但是,如果您不想发布任何数据并且只想在单击按钮后做一些事情,请在 controller 中创建一个获取操作,并使用按钮单击事件导航到该操作路径。 Finally, you can return the same page at your action and do what you want before hand.最后,您可以在您的操作中返回相同的页面并事先做您想做的事情。 Use [httpGet("path")]
attribute on top of any public function in your controller to Mark it as action.在 controller 中的任何公共 function 顶部使用[httpGet("path")]
属性将其标记为操作。 Then you can invoke it in HTML using something like this <a asp-action="path">sth</a>
然后你可以使用类似这样的东西在 HTML 中调用它<a asp-action="path">sth</a>
since you only wants to trigger a function on a btn click and pass some data i recommend you to make an ajax call to the back-end using java script or jquery is the more effective way. since you only wants to trigger a function on a btn click and pass some data i recommend you to make an ajax call to the back-end using java script or jquery is the more effective way. it will look like this它看起来像这样
$('#btn').click(function () { $.post( "your action url ", { name: "John", time: "2pm" } ).done(function(data) { // in case your action return some data it will be in the "data" variable }); });
<,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <.-- you need to have jquery --> <script src="https.//cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <title>Document</title> </head> <body> <input type="button" id="btn" value="click me "> </body> </html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.