繁体   English   中英

从JavaScript调用控制器方法

[英]Call controller method from javascript

我想从jquery调用中调用controller方法。 这是我的code

<script>
function showImage(id) {
    $.get('/Canvas/getPath?id=' + id, function (data) {
        if (data != '') {
            var ext = /[^.]+$/.exec(data);
            var filename = data;
            if(ext == 'tif')
            {                    
                //window.open('/DocViewer/DocViewer.aspx?FilePath=' + data, 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                window.open('/DocViewer/DocViewer.aspx', 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
            }
            else if(ext == 'xml'){
                $.ajax('/Canvas/getXml?filePath=' + filename, function (xmldata) {
                    alert(xmldata);
                    window.open(xmldata, "WindowPopup", 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                }); 
            }
        }
        else {
            alert('Document does not exists. Please make sure the document is on location.');
        }
    });
};
</script>

编辑

这是我的控制器方法

    public JsonResult getXml(string filePath)
    {
        if (System.IO.File.Exists(filePath))
        {
            string xmlString = System.IO.File.ReadAllText(filePath);
            return Json(xmlString, JsonRequestBehavior.AllowGet); ;
        }
        else
        {
            return Json("", JsonRequestBehavior.AllowGet);
        }
    }

在这里,如果要ext if xml,我想要什么,我想调用controllermethod 在上面的代码中,它给了我关于new { filePath = filename }文件名不存在的错误。 基本上我试图在浏览器中打开xml文件 在数据中,我收到xml文件的filePath 如何调用控制器方法或在浏览器中打开xml文件?

$.ajax({
      url: '@Url.Action("getXml", "Canvas")'+'?filePath'+=filename,
      type: 'GET',
      success: function(data) 
      { 
      alert(data); 
      }
});

您的代码无法正常工作,并且未定义参数,因为您混用了javascript和razor方法调用。

function showImage(id) {
    $.get('/Canvas/getPath?id=' + id, function (data) {
        if (data != '') {
            var ext = /[^.]+$/.exec(data);
            var filename = data;
            if(ext == 'tif')
            {                    
                //window.open('/DocViewer/DocViewer.aspx?FilePath=' + data, 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                window.open('/DocViewer/DocViewer.aspx', 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
            }
            else if(ext == 'xml'){
                $.ajax({
                url: '@Url.Action("getXml", "Canvas")'+'?filePath'+=filename,
                type: 'GET',
                success: function(data) 
                { 
                alert(data); 
                }
                });
                //window.open(data, "WindowPopup", 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
            }
        }
        else {
            alert('Document does not exists. Please make sure the document is on location.');
        }
    });
};

大家好,我的问题解决了。 这是我的完整script

<script>
function showImage(id) {
    $.getJSON('/Canvas/getPath?id=' + id, function (data) {
        if (data != '') {
            var ext = /[^.]+$/.exec(data);
            var filename = data;
            if(ext == 'tif')
            {
                //window.open('/DocViewer/DocViewer.aspx?FilePath=' + data, 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                window.open('/DocViewer/DocViewer.aspx', 'WindowPopup', 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
            }
            else if(ext == 'xml'){                    
                $.getJSON('/Canvas/getXml?filePath=' + filename, function (xmldata) {
                    alert(xmldata);
                    window.open('data:text/xml,' + encodeURIComponent(xmldata), "WindowPopup", 'height=auto,width=auto,status=no,toolbar=no,resizable=yes,scrollbars=yes');
                });                    
            }
        }
        else {
            alert('Document does not exists. Please make sure the document is on location.');
        }
    });
};

暂无
暂无

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

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