Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New window opens on click of actionLink

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.

like image 855
14578446 Avatar asked Dec 13 '11 20:12

14578446


2 Answers

Try This

<%=Html.ActionLink(a.Title, "View", "Report", new { Id = a.id.ToString() }, new { target="_blank" })%> 
like image 159
sohaib Avatar answered Nov 18 '22 07:11

sohaib


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.

like image 41
bobek Avatar answered Nov 18 '22 06:11

bobek