New window opens on click of actionLink
Asked Answered
T

6

5

I have a requirment to invoke a controller method from the view page. On click of the link below the method should be invoked.

@Html.ActionLink(item.InvoiceNumber, "SendPdfStatement", "Invoice", 
                 new { item.InvoiceNumber }, new { target = "_blank" })

the method signature is as:

public void SendPdfStatement(string InvoiceNumber)
    {

        InvoiceNumber = InvoiceNumber.Trim();

        ObjectParameter[] parameters = new ObjectParameter[1];
        parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);

        List<Models.Statement> list = new List<Models.Statement>();
        list = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters).ToList<Models.Statement>();

        var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters);
        Models.Statement statement = statementResult.SingleOrDefault();

        pdfStatementController.WriteInTemplate(statement);                                 

   }

now the problem is when i click on the link, a blank window opens. I know it is something with new { target = "_blank" }. If i pass null in its place my page with link becomes blank. What shall i pass here so the my page remains as it is and no new blank window also appears.

Transpolar answered 13/12, 2011 at 20:2 Comment(2)
The problem is in your controller method, not in this html helper invocation. It appears that your method is returning an empty result. You should show us the missing code. By the way, your method should be called SendPdfStatement, in a InvoiceController class. Are you sure are you checking the right one?Conchitaconchobar
the method is called SendPdfStatement only in InvoiceController. Have put the detail code for methodTranspolar
K
2

Change your controller Action. The page that you get is blank because you are not returning anything. Do

 public ActionResult SendPdfStatement(string InvoiceNumber)

    {
    InvoiceNumber = InvoiceNumber.Trim();

        ObjectParameter[] parameters = new ObjectParameter[1];
        parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);

        List<Models.Statement> list = new List<Models.Statement>();
        list = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters).ToList<Models.Statement>();

        var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters);
        Models.Statement statement = statementResult.SingleOrDefault();

        pdfStatementController.WriteInTemplate(statement); 

    return View();
    }

EDIT: Or you should use AJAX so that your page is not reloaded and you don't have to return anything from your method. Read here http://www.asp.net/mvc/overview/older-versions-1/contact-manager/iteration-7-add-ajax-functionality-cs.

Karly answered 13/12, 2011 at 20:21 Comment(4)
but i dont hv anything to return hereTranspolar
You do. You are returning a View so that something can be displayed on the page. You can do return View('pathhere');Karly
my original view requires a model as input which will not be available here inside "SendPdfStatement"Transpolar
see my edit. You have to use AJAX so that you don't reload the page. Then your link will have jquery function call 'onclick="savePdf();"' which will then call the HttpPost controller actionKarly
F
6

Try This

<%=Html.ActionLink(a.Title, "View", "Report", new { Id = a.id.ToString() }, new { target="_blank" })%> 
Firecracker answered 4/8, 2013 at 5:47 Comment(0)
K
2

Change your controller Action. The page that you get is blank because you are not returning anything. Do

 public ActionResult SendPdfStatement(string InvoiceNumber)

    {
    InvoiceNumber = InvoiceNumber.Trim();

        ObjectParameter[] parameters = new ObjectParameter[1];
        parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);

        List<Models.Statement> list = new List<Models.Statement>();
        list = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters).ToList<Models.Statement>();

        var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters);
        Models.Statement statement = statementResult.SingleOrDefault();

        pdfStatementController.WriteInTemplate(statement); 

    return View();
    }

EDIT: Or you should use AJAX so that your page is not reloaded and you don't have to return anything from your method. Read here http://www.asp.net/mvc/overview/older-versions-1/contact-manager/iteration-7-add-ajax-functionality-cs.

Karly answered 13/12, 2011 at 20:21 Comment(4)
but i dont hv anything to return hereTranspolar
You do. You are returning a View so that something can be displayed on the page. You can do return View('pathhere');Karly
my original view requires a model as input which will not be available here inside "SendPdfStatement"Transpolar
see my edit. You have to use AJAX so that you don't reload the page. Then your link will have jquery function call 'onclick="savePdf();"' which will then call the HttpPost controller actionKarly
E
1
<input type="button" class="btn btn-primary" value="Text" onclick="window.open('@Url.Action("Action", "Controller")',target='_blank');return false;"/>         

 <button type="button" class="goBtn btn btn-primary btn-mid" onclick="window.open('@Url.Action("CheckInReport", "Staff")', target='_blank')" id="CheckIn">Check In</button>

This works for MVC5 as well

Emilie answered 27/9, 2013 at 11:7 Comment(0)
Q
0

try passing _self instead of _blank or alternatively passing null instead of , new { target = "_blank" }

i.e. @Html.ActionLink(item.InvoiceNumber, "SendPdfStatement", "Invoice", new { item.InvoiceNumber }, null )

or leave off the new { target = "_blank" } all together

Quidnunc answered 13/12, 2011 at 20:7 Comment(1)
passing _self or null makes the original window blank. Leaving { target = "_blank" } raises exception as new { item.InvoiceNumber } is taken as the html parameter.Transpolar
T
0

First of all

TARGET = "_blank"

Is used to open hyperreferenced resource in a new browser window, so if you don't want the new window - why would you put that in place ? :-)

Second, look at the ActionLink helper method (description I took from question referenced bellow):

         Html.ActionLink(article.Title, 
            "Item",   // <-- ActionMethod
            "Login",  // <-- Controller Name.
            new { article.ArticleID }, // <-- Route arguments.
            null  // <-- htmlArguments .. which are none. You need this value
                  //     otherwise you call the WRONG method ...
                  //     (refer to comments, below).
            )

take a look at HTML.ActionLink method

I assume you're hitting the wrong method overload if you get errors. If you replace htmlArguments with null, you should be good to go, but as your method returns VOID (nothing) you will get an empty page (what else would you expect :) ? )

To cancel the default navigation mechanism you can implement simple jquery rule :

$('a.invoicelinkclass').click(function(e){
    e.preventDefault();
    $.get($(this).attr('href'),function(){
       // maybe an alert() or jquery ui .dialog() to let user know that something happened ?
    });
 });
Taliped answered 13/12, 2011 at 20:25 Comment(2)
just want to get rid of the opening blank pageTranspolar
you can use javascript (I've updated my answer) but it would be good to let user know if his action succeed or notPerspex
I
0
@Html.ActionLink(item.InvoiceNumber, "SendPdfStatement", "Invoice", 
                 new { item.InvoiceNumber }, null);

So basically that last parameter is your html attributes, if you don't need it to open in a new window...that's the only attribute in there so just pass null for the whole parameter.

Instance answered 4/8, 2013 at 7:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.