简体   繁体   English

如何从 my.cshtml 页面中的按钮调用 controller 中的 function?

[英]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.

相关问题 如何以URL格式将GET参数从一个cshtml页面传输到另一个cshtml页面? - How do I transfer a GET-Parameter from one cshtml-page to another in a URL-Format? 如何从 Z9E0DA8438E1E38A1C30F4B76CE73B8 页面调用我的 API controller object? - How do I call my API controller object from an ASP.NET Core page? 如何从MVC控制器中的进程刷新cshtml文件? - How can I refresh my cshtml file from a process in my MVC Controller? 如何从我的角度调用另一个 controller - How do I call another controller from my view 如何在 Layout.cshtml 中引用我的包 - How do I reference my packages in my Layout.cshtml 如何将我的ID从按钮发送到控制器? - How can I get my ID from button to my controller? 如何使用 Razor 从我的视图中调用控制器操作? - How do I call a controller action from within my view using Razor? 如何从一个cshtml文件调用或使用cshtml文件的功能? - How to call or use function of cshtml file from one cshtml file? 在单击按钮事件中,如何将参数传递给来自我网页的BasePage的javaScript函数? - how do I pass a parameter to a javaScript function which comes from the BasePage of my web page, on a click button event? 如何将变量从View传递到Controller? - How do I pass a variable from my View to my Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM