A new window opens by clicking actionLink

I have a requirement to call the controller method from the view page. When you click the link below, the method should be called.

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

The method signature looks like this:

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, an empty window opens. I know this is something with new { target = "_blank" }. If I submit null, then my page with the link will be empty. What can I pass so that my page remains as it is, and a new blank window does not appear.

+5
source share
6 answers

. , , , .

 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: AJAX, , . http://www.asp.net/mvc/overview/older-versions-1/contact-manager/iteration-7-add-ajax-functionality-cs.

+2

<%=Html.ActionLink(a.Title, "View", "Report", new { Id = a.id.ToString() }, new { target="_blank" })%> 
+6
<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>

MVC5

+1

_self _blank null new { target = "_blank" }

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

new { target = "_blank" }

0

TARGET = "_blank"

, , - ?: -)

-, ActionLink (, ):

         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).
            )

HTML.ActionLink

, , . htmlArguments null, , , VOID (), ( :)? )

, jquery:

$('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 ?
    });
 });
0
@Html.ActionLink(item.InvoiceNumber, "SendPdfStatement", "Invoice", 
                 new { item.InvoiceNumber }, null);

So basically the last parameter is your html attributes, unless you need to open it in a new window ... that the only attribute there so simply passes the null value for the whole parameter.

0
source

All Articles