C# 单击 HTML 按钮(不提交或表单的发布按钮)时调用控制器的特定操作 Asp.net MVC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15159244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:19:11  来源:igfitidea点击:

Calling a specific action of a controller on the click of HTML button(Not Submit or Form's post Button) Asp.net MVC

c#asp.netasp.net-mvcasp.net-mvc-3asp.net-mvc-4

提问by Yogesh

When we click a Form's submit button, then action of the controller which is having HTTPPost attribute is called but what if i want to call or perform an action when a normal HTML button is clicked Although following articles

当我们单击表单的提交按钮时,将调用具有 HTTPPost 属性的控制器的操作,但是如果我想在单击普通 HTML 按钮时调用或执行操作怎么办,尽管以下文章

http://www.codeproject.com/Tips/198477/Calling-a-MVC-Controller-and-Action-Method-using-H

http://www.codeproject.com/Tips/198477/Calling-a-MVC-Controller-and-Action-Method-using-H

HTML button calling an MVC Controller and Action method

调用 MVC 控制器和操作方法的 HTML 按钮

tells the approach but both of these are using controller name in view, So view has to know about controller, I am looking for an answer that view wont have to know about controller. because views has to be Independent from Controller, Views should not know about controller So, if you know the answer then please reply

告诉方法但是这两个都在视图中使用控制器名称,所以视图必须知道控制器,我正在寻找一个视图不必知道控制器的答案。因为视图必须独立于控制器,视图不应该知道控制器所以,如果你知道答案,请回复

采纳答案by Dave Alperovich

any form that directs your user to url created by

任何将您的用户定向到由

<a href='@Url.Action("{action}", "{controller}")'> click me </a> 

or

或者

@using(BeginForm("{action}", "{controller}")

will do what you want.

会做你想做的。

That can be with a

这可以与

  • form
  • button link
  • 形式
  • 按钮链接

It's the destination that matters. The View does not "know" anything about the action or controller. The helper does.

重要的是目的地。视图“不知道”有关操作或控制器的任何信息。帮手做的。

回答by Ben

To execute an MVC action from the client side (i.e. from a view) you need to hit a URL (with any verb: get, post, put, etc). Therefore to execute an action form a view you will need to know the URL of that action, by default that URL is directly mapped to the controllername/actionnamebut you can re-define this if you want to create more abstraction between view and controller.

要从客户端(即从视图)执行 MVC 操作,您需要点击一个 URL(使用任何动词:get、post、put 等)。因此,要从视图执行操作,您需要知道该操作的 URL,默认情况下该 URL 直接映射到 ,controllername/actionname但如果您想在视图和控制器之间创建更多抽象,则可以重新定义它。

Given this your button just needs to be a link to a URL or linked to js to do an Ajax http request.

鉴于此,您的按钮只需是指向 URL 的链接或链接到 js 即可执行 Ajax http 请求。

Hope that helps.

希望有帮助。

回答by Darin Dimitrov

You cannot have 2 actions on the same controller with the same name and the same HTTP verb. So what you are asking doesn't make sense. You could invoke the same controller action as the one that rendered the view without specifying an action and controller name. The reason why Html.BeginForm()works without specifying an action and controller name is because the form is sending a POST request to the server and you can distinguish the 2 actions.

您不能在同一个控制器上有 2 个具有相同名称和相同 HTTP 动词的操作。所以你问的没有意义。您可以调用与呈现视图相同的控制器操作,而无需指定操作和控制器名称。之所以Html.BeginForm()没有指定动作和控制器的名称作品是因为形式发送POST请求到服务器,你可以分辨出2个动作。