call action method in the controller through a view script

1

I have an action inside my controller, to download an XML file, with the following signature.   public ActionResult Export (int id) {}, I do not understand in 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 this?

        //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;
        }       
    }

How would the view script look?

            downloadParameter: function (e) {
                 ??
            },
    
asked by anonymous 11.01.2017 / 13:49

1 answer

1

I believe the best way is to make the call via javascript itself. 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"); }
    
11.01.2017 / 21:55