简体   繁体   English

如何从按钮单击asp.net调用静态方法

[英]How to call a static method from a button click asp.net

I have a method that follows: 我有以下方法:

public static void UpdateAll()
    {
       // Updates database
    }

and I have a MVC Application, the view is as follows (removed lines for clarity): 并且我有一个MVC应用程序,视图如下(为清晰起见,删除了几行):

<button id ="btn">Update</button>
//...
<script>
    $('#btn').click(function () {
        @{
           project.models.settings.UpdateAll();
         }
    });
</script>

The project.models.settings.UpdateAll() method fires as soon as the page loads. 页面加载后立即触发project.models.settings.UpdateAll()方法。 Is there a way of making this method load, only once the user has clicked the button? 只有在用户单击按钮后,才可以加载此方法吗?

To test I did replace the method with a simple console.log and that worked as intended. 为了进行测试,我确实用一个简单的console.log替换了该方法,并且该方法按预期工作。

Any pointers, examples are much appreciated. 任何指针,示例都将不胜感激。

Kind regards 亲切的问候

I think you're missing a fundamental understanding of how web applications work. 我认为您缺少对Web应用程序如何工作的基本了解。

When you put the following into your view you are declaring a chunk of server-side code that will be executed while the view is being processed on the server-side. 当您将以下内容放入视图时,您将声明将在服务器端处理视图时执行的服务器端代码块。

@{
    project.models.settings.UpdateAll();
}

The reason that it works when you replace it with a console.log is that console.log is client-side code and it executes on the client (ie in the browser). 当用console.log替换它时,它起作用的原因是console.log是客户端代码,它在客户端(即在浏览器中)执行。

The only way you are going to call server-side code (your UpdateAll method) from client-side code (your button click event handler) is by making an AJAX call. 从客户端代码(按钮单击事件处理程序)调用服务器端代码(您的UpdateAll方法)的唯一方法是进行AJAX调用。

The simplest thing would be to first expose your UpdateAll method as a controller action. 最简单的事情是首先将UpdateAll方法公开为控制器操作。 Generally for actions that respond to AJAX calls I like to create a separate controller but that's not absolutely necessary. 通常,对于响应AJAX调用的操作,我喜欢创建一个单独的控制器,但这不是绝对必要的。

public ServiceController : Controller
{
    public ActionResult UpdateAll()
    {
    }
}

Now from your client-side code make an AJAX call to that action. 现在,从客户端代码对该操作进行AJAX调用。

the way you are doing will not work. 您所做的方式将无法正常工作。

the method will be called when view loads as it is server side code and it will be executed on view rendering time. 当视图加载时将调用该方法,因为它是服务器端代码,并且将在视图渲染时执行。

so call an action using ajax and in that action call this method: 因此,请使用ajax调用一个操作,然后在该操作中调用以下方法:

$('#btn').click(function () {
         $.ajax({
                  type: "GET",
                  url: '@Url.Action("MyAction","My")',
                  success: function(data) {

                   },
                   error: function(result) {
                    alert("Error");
                    }
         });
    });

write an action in controller: 在控制器中编写一个动作:

public void MyAction()
{
project.models.settings.UpdateAll();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM