简体   繁体   English

使用mvc2和c#,如何从视图中调用控制器中的void方法

[英]Using mvc2 and c#, how do I call a void method in my controller from view

I am trying to make a button in html that when it is clicked, it calls a method that is in my controller that will create a pdf. 我试图在html中创建一个按钮,当单击该按钮时,它将调用控制器中的一个方法,该方法将创建pdf。

my controller is DashboardController, my method is called PrintConfirm() 我的控制器是DashboardController,我的方法叫做PrintConfirm()

here is my html: 这是我的HTML:

<a onclick="<%:Url.Action("PrintConfirm")/%>fileName" rel="external" target="_blank">
    <button class="ui-icon ui-icon-print" data-role ="button">Print Confirmation</button>
</a>

When I run it, all I get is a printer icon and nothing happens when I click on it 当我运行它时,我得到的只是一个打印机图标,而当我单击它时什么也没有发生

Your gator tag is incorrect, and your onclick should be href . 您的gator标签不正确,您的onclick应该是href Try this: 尝试这个:

<a href='<%=Url.Action("PrintConfirm")%>/filename' rel="external" target="_blank">
    <button class="ui-icon ui-icon-print" data-role ="button">Print Confirmation</button
</a>

In addition, you cannot call a void method from the view. 此外,您不能从视图中调用void方法。 Only actions may be invoked, and actions must have a return type of ActionResult . 只能调用操作,并且操作必须具有ActionResult的返回类型。

Also, if your PrintConfirm action is on different controller than the one which returned the current view, you'll need to specify that controller's name as well. 另外,如果您的PrintConfirm操作与返回当前视图的控制器PrintConfirm同一控制器上,则还需要指定该控制器的名称。

<%=Url.Action("PrintConfirm", "PdfPrinter") /* where the controller is PdfPrinterController */%> 

Edit 编辑

If you were just trying to invoke the action asynchronously, you'll probably want to use jQuery and AJAX. 如果您只是尝试异步调用该动作,则可能要使用jQuery和AJAX。 Let me know if you'd like an example. 让我知道您是否想举个例子。

Onclick should be replaced to href first of all 首先应将Onclick替换为href

Onclick is an event that can be handled by JavaScript code so in your case it will just do nothing as Onclick will contain relative URL of the action instead of js code. Onclick是可以由JavaScript代码处理的事件,因此在您的情况下,它不会执行任何操作,因为Onclick将包含操作的相对URL而不是js代码。 But also you don't have to use it, just regular href attribute. 但您也不必使用它,只需使用常规href属性。

Look at the generated code. 查看生成的代码。 You'll see something like 您会看到类似

<a onclick="/Dashboard/PrintConfirmfilename" rel="external" target="_blank">

am I right? 我对吗? onclick is expecting javascript. onclick需要使用javascript。

Let's figure this out together... 让我们一起解决这个问题...

What does Url.Action() return? Url.Action()返回什么? A Url. 网址。

<a> tags can be used to either 1) navigate the user to a URL (in this case you use the hre= attribute) and/or 2) run some javascript (in this case you put the name of a javascript function in the onclick attribute). <a>标记可用于1)将用户导航到URL(在这种情况下,您使用hre =属性)和/或2)运行一些javascript(在这种情况下,您将javascript函数的名称放在onclick属性)。

What you've done here is put a url in onclick which just doesn't work. 您在此处所做的是将一个URL放在onclick中,但这根本不起作用。

The only way that I know of doing this is using AJAX 我唯一知道的方法是使用AJAX

 $.ajax({ type: "GET",
        url: "./PrintConfirm",
        success: function (data) {
            //Load your PDF in the appropriate div? here
        }
    });

Otherwise, you have to make the call to the controller and reload the screen appropriately. 否则,您必须致电控制器并适当地重新加载屏幕。

Url.Action is going to return a URL that points to an action in a controller. Url.Action将返回指向控制器中动作的URL。 As such, you'll end up with something like this: 这样,您最终将得到如下结果:

<a onclick="/myController/PrintConfirmfilename" rel="external" target="_blank">...

There are a couple of problems here: 这里有两个问题:

  1. The proper URL to the action is probably /myController/PrintConfirm, not /myController/PrintConfirmfilename. 该操作的正确URL可能是/ myController / PrintConfirm,而不是/ myController / PrintConfirmfilename。 The extra 'filename' seems weird. 多余的“文件名”似乎很奇怪。

  2. It seems odd to have a HTML button tag inside an a tag. 在标签中包含HTML按钮标签似乎很奇怪。 I would recommend choosing one or the other. 我建议选择一个。

Assuming that /myController/PrintConfirm actually points to the action that produces the PDF you want, here are some options: 假设/ myController / PrintConfirm实际上指向生成所需PDF的动作,则有一些选择:

<a href=<%:Url.Action("PrintConfirm")>>
   Print Confirmation
</a>

or 要么

<button onclick=window.location=<%:Url.Action("PrintConfirm")>
   Print Confirmation
</button>

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

相关问题 C#MVC 4使用Controller中的Javascript调用Void - C# MVC 4 Call Void using Javascript in Controller 如何在C#中调用异步方法从同步方法返回void并确保异步方法完成? - How do I call asynchronous method returning void from synchronous method in C# and be sure async method is finished? 如何使用 ASP.NET MVC C# Html 从视图中调用控制器中的函数 - How to call a function in the controller from the view using ASP.NET MVC C# Html 如何从我的角度调用另一个 controller - How do I call another controller from my view MVC2 ListBox没有从视图绑定到控制器 - MVC2 ListBox not binding from View to Controller 从同一控制器ASP.NET MVC3重定向以查看或调用void方法 - Redirecting to view or call void method from same controller ASP.NET MVC3 如何在MVC2中将数据从子视图传递到父视图? - How do I pass data from child view to parent view in MVC2? C# - 如何从静态void main调用方法 - C# - How to call a method from static void main ASP.NET MVC - 如何从视图中调用 Controller 方法以重定向到多个其他视图? - ASP.NET MVC - How do I Call a Controller Method from within a View to Redirect to Multiple Other Views? ASP.NET MVC - 如何在不离开视图的情况下调用 void 控制器方法? - ASP.NET MVC - How to call void controller method without leaving the view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM