简体   繁体   中英

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.

my controller is DashboardController, my method is called PrintConfirm()

here is my 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 . 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. Only actions may be invoked, and actions must have a return type of 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.

<%=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. Let me know if you'd like an example.

Onclick should be replaced to href first of all

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. But also you don't have to use it, just regular href attribute.

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.

Let's figure this out together...

What does Url.Action() return? 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).

What you've done here is put a url in onclick which just doesn't work.

The only way that I know of doing this is using 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. 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. The extra 'filename' seems weird.

  2. It seems odd to have a HTML button tag inside an a tag. 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:

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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