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.
try passing
_self
instead of_blank
or alternatively passingnull
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 togetherThis works for MVC5 as well
Change your controller Action. The page that you get is blank because you are not returning anything. Do
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.
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.
Try This
First of all
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):
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 :