call action method in the controller via a view script

Asked

Viewed 1,393 times

1

I have an action inside my controller, to download an XML file, with the following signature. public Actionresult Export(int id){}, no understand on my page I have a button that calls a download script in the view, I need this script to trigger the export method inside my controller passing id as parameter. How do I access?

        //Método do controller
    public ActionResult Export(int id)
    {

        var resource = WorkCenterFlow.GetResource(id);
        using (var stream = new System.IO.MemoryStream())
        {
            resource.Export(stream);
            var result = new FileContentResult(stream.ToArray(), "application/octet-stream");
            result.FileDownloadName = string.Format("{0}.bin", (resource.FullName ?? "File").Replace(' ', '_').TrimEnd().TrimStart());
            return result;
        }       
    }

What the view script would look like?

            downloadParameter: function (e) {
                 ??
            },

1 answer

1


I believe the best way is to make the call via javascript same. Something like

$("#btnDownlaod").click(function (e) {
    callPostBack(e, this, @Url.Action("Export", "Nome_Controller", new { id = id_valor }, Request.Url.Scheme), null, SuccessoCallback, ErroCallback);
});

function SuccessoCallback (data) { alert("deu certo"); }

function ErroCallback (MsgErro) { alert("deu errado"); }
  • Thanks man, it worked like this!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.