简体   繁体   English

用jQuery调用MVC控制器

[英]Call mvc controller with jQuery

I'm pretty new to mvc and jQuery... and to web development in general really. 我对mvc和jQuery ...以及一般而言的Web开发都是很新的。 I recently took over control of a site designed by a developer that we had to let go and the treeview control he had used needed to be changed for reasons that I won't get into here. 最近,我接管了一个由开发人员设计的网站的控制权,而我们不得不放弃该控制权,出于我不愿透露其原因的原因,需要更改他使用的树形视图控件。 But basically I need to be able to have resources be download on the click event of a node in the treeview. 但是基本上,我需要能够在树视图中的节点的click事件上下载资源。 I know how to handle the event but I can't figure out how to make the call to my mvc controller through jquery. 我知道如何处理事件,但我不知道如何通过jquery对我的mvc控制器进行调用。 The path to the controller function I need to call to download the resource is /Resources/DownloadResource. 我需要调用以下载资源的控制器功能的路径是/ Resources / DownloadResource。 Here's the code for it: 这是它的代码:

        public ActionResult DownloadResource(string id)
        {
            var resource =
                _resourceService.GetResourceQuery(new Specification<Resource>(r => r.ResourceId == new Guid(id))).FirstOrDefault();

        return new BinaryResult
        {
            FileName = resource.FileName,
            ContentType = string.Format("application/{0}", Path.GetExtension(resource.FileName)).Replace(".", ""),
            IsAttachment = true,
            Data = System.IO.File.ReadAllBytes(resource.FilePath)
        };
    }

I have tried something like $.post("/Resources/DownloadResourceLink", { id: value }); 我已经尝试过类似$.post("/Resources/DownloadResourceLink", { id: value }); and when I step through, everything is getting the correct values, but no download. 当我逐步执行时,所有内容都获得正确的值,但没有下载。 Any help would definitely be appreciated! 任何帮助将不胜感激!

To trigger the download from the browser, you need to use a synchronous way not ajax. 要从浏览器触发下载,您需要使用同步方式而不是ajax。

Look at this question: Downloading a file onto client in ASP.NET MVC application using JQuery 看一下这个问题: 使用JQuery将文件下载到ASP.NET MVC应用程序中的客户端上

<script type="text/javascript">

    $(document).keypress(function (e) 
    {
        if (e.which == 13) 
        {
             location.href = '@Url.Action("ActionName", "Controllername")';
        }
    });

</script>

尝试看一下基本上,您只是将MVC操作地址称为ajax函数的url属性。

The problem may be how you are passing in the id. 问题可能是您如何传递ID。 In my experience, I have to append the data into the url (/action/controller/id route) 以我的经验,我必须将数据附加到url (/ action / controller / id路由)中

$.post("/Resources/DownloadResourceLink/" + value);

If that doesn't work, I'd make sure the content type the call is expecting is correct for the download type. 如果那不起作用,我将确保呼叫期望的内容类型对于下载类型是正确的。

If that still doesn't do it, then the issue may be the post. 如果仍然不能解决问题,则可能是问题所在。

I recommend you take a look at $.ajax() . 我建议您看一下$.ajax() The default is a get, which you may need for a download. 默认值为获取,您可能需要下载该获取。 I don't know what the content type needs to be (you should be able to infer that from the action method), but your call would look something like this. 我不知道内容类型需要是什么(您应该能够从action方法推断出来),但是您的调用看起来像这样。

$.ajax({
    url: '/Resources/DownloadResourceLink',
    params: { id }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM