在单击HTML按钮(不提交或表单的帖子按钮)Asp.net MVC上调用控制器的特定操作

时间:2022-11-24 09:41:00

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

当我们单击Form的提交按钮时,会调用具有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

HTML按钮调用MVC控制器和Action方法

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

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

3 个解决方案

#1


16  

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.

这是重要的目的地。 View不会“知道”有关动作或控制器的任何信息。帮手呢。

#2


0  

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/actionname but 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.

希望有所帮助。

#3


0  

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.

您不能在具有相同名称和相同HTTP谓词的同一控制器上执行2个操作。所以你问的是没有意义的。您可以调用与呈现视图的控制器操作相同的控制器操作,而无需指定操作和控制器名称。 Html.BeginForm()在不指定操作和控制器名称的情况下工作的原因是表单正在向服务器发送POST请求,您可以区分这两个操作。

#1


16  

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.

这是重要的目的地。 View不会“知道”有关动作或控制器的任何信息。帮手呢。

#2


0  

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/actionname but 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.

希望有所帮助。

#3


0  

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.

您不能在具有相同名称和相同HTTP谓词的同一控制器上执行2个操作。所以你问的是没有意义的。您可以调用与呈现视图的控制器操作相同的控制器操作,而无需指定操作和控制器名称。 Html.BeginForm()在不指定操作和控制器名称的情况下工作的原因是表单正在向服务器发送POST请求,您可以区分这两个操作。