控制器通过asp.net MVC 5呈现rdlc报告并开始使用表单助手

问题描述

我正在使用asp.net MVC 5,并希望: 1-要通过控制器渲染和打开rdlc报告,我已经通过创建Web表单页面并通过它渲染报告来完成相同的工作,但是我知道这对我来说不是最好的。
2-当我的报告加载时,我想要的另一件事是,我想要消息或提示询问我ID号是什么。

这是我的空控制器 公共类ReportsController:控制器 {

    // GET: /Home/Export
    public ActionResult Index()
    {
        return View();
    }

我项目中的报告路径 href =“〜/ Reports / Reports / Salaries_Details_Card.rdlc” 和数据集路径 href =“〜/ Reports / Dataset / Salaries_AllowansesDataSet.xsd”

我想要的是 1-在该控制器中执行的操作,以在我的项目中呈现本地报告,并从我的数据库传递名为CODE_ID的参数。 2-在“ Begin form helper”视图中,以加载报告并通过提示符传递要求CODE_ID的参数 我想要的代码如下所示,但我不知道该怎么做

//
public ActionResult PRF(int id)
        {
            string fileType = "PDF";
            LocalReport lr = new LocalReport();
            string path = Path.Combine(Server.MapPath("~/Reports"),"PRF.rdlc");
            if (System.IO.File.Exists(path)) { lr.ReportPath = path; }
            else
            {
                return View("Index");
            }
            var adof = db.Config.Find(9).ConfigValue;
            var cm = db.PRFs.Where(x => x.ID== id).Select(z =>
            new {
                StaffName = z.StaffList.StaffName,IndexNo = z.IndexNo,ID = "PRF" + z.ID,CreateDate = DateTime.Now,WINGSAMMOUNT = z.Ammount,WINGSTripNO = z.Comments,SentToFianceBy = User.Identity.Name.ToString().Replace("GLOBAL\\","").Replace("."," ").Replace("mhdiyad","Iyad"),SentToFianceDate = z.Act_Date,AdminOficer = adof

            }).ToList();

            //{ cm = db.Trip.Where(x => listOfSearchedIds.Contains(x.ID)).OrderBy(y => y.ID).ToList(); }
            ReportDataSource rd = new ReportDataSource("DataSet1",cm);
            lr.DataSources.Add(rd); string reportType = fileType;
            string mimeType; string encoding; string fileNameExtension;
            string deviceInfo = "<DeviceInfo>" + " <OutputFormat>" + fileType + "</OutputFormat>" + " <PageWidth>210 mm</PageWidth>" + " <PageHeight>280 mm</PageHeight>" + " <MarginTop>1 mm</MarginTop>" + " <MarginLeft>1 mm</MarginLeft>" + " <MarginRight>1 mm</MarginRight>" + " <MarginBottom>1 mm</MarginBottom>" + "</DeviceInfo>";
            Warning[] warnings; string[] streams;
            byte[] renderedBytes;
            renderedBytes = lr.Render(reportType,deviceInfo,out mimeType,out encoding,out fileNameExtension,out streams,out warnings);
            //return File(renderedBytes,mimeType,"PRF_" + id.Replace(",","-"));
            return File(renderedBytes,mimeType);

        }
public ActionResult DeleteRole(string RoleName)
        {
            System.Web.Security.Roles.DeleteRole(RoleName);
            return RedirectToAction("Index");
        }
@Html.ActionLink("Delete","DeleteRole","ManageAccess",new { RoleName = @role },new { @Class = "btnInActive" }) 

解决方法

RDLC报告可以选择呈现为PDF,如果您不想使用Report Viewer控件(它需要WebForms页面或使用WebPages语法的视图),这将非常有用-可以,如果担心的话,可以使用路由来“隐藏” .aspx扩展名。

假设您不打算保留对不再受其供应商支持的浏览器的支持,则可以使用embed:

<embed id="report-viewer" src="" style="width: 100%; min-height: 1200px;" />

或iframe

<iframe id="report-viewer" src="" style="width: 100%; min-height: 1200px;"></iframe>

任何一种都可以利用浏览器的内置PDF渲染引擎。主要区别在于您是希望PDF直接受到页面上其他内容(嵌入)的影响还是被隔离(iframe)。两者都是有效的,并且是HTML5规范的一部分(请注意,我不是是指框架或框架集-已弃用)。

如果您确实需要某种传统支持,则可以使用PDF.js或类似的方法来嵌入PDF查看器。看起来像这样:

<iframe id="report-viewer" src="~/pdfjs/web/viewer.html?file=YOUR_ACTION_URL_HERE" style="width: 100%; min-height: 1200px;"></iframe>

不确定嵌入是否适用于PDF.js,但我认为可以。

我不是非常喜欢在页面加载时立即向用户的脸部发出警报的狂热爱好者,但是类似的事情可能会使您关闭(我假设jQuery可用,但是普通的JavaScript并没有太大的区别) :

$(function() {
    // Select the viewer element - this won't change,so keep a reference to it
    let $viewer = $('#report-viewer');
    
    // Set up the base of our updated src URL
    let url = '@Url.Action("prf")';
    
    // Ask for CODE_ID value --> should probably add some validation here
    let id = prompt('Code?');
    if(id) {
        // If value of id is *something*,append it to base URL and update the viewer's src attribute
        url += '?id=' + id;
        
        $viewer.attr('src',url);
    }
});

如果您使用的是PDF.js或类似文件,则可以在设置src属性之前,修改上面的代码以将PDF.js的URL附加到更新的URL之前:

$(function() {
    // Select the viewer element - this won't change,so keep a reference to it
    let $viewer = $('#report-viewer');
    
    // Set up the base of our updated src URL
    // Include the PDF.js URL at the beginning
    let url = '/pdfjs/web/viewer.html?file=@Url.Action("prf")';
    
    // Ask for CODE_ID value --> should probably add some validation here
    let id = prompt('Code?');
    if(id) {
        // If value of id is *something*,url);
    }
});

embed / iframe上的样式属性是任意的-我选择了这一点,因为它或多或少允许PDF呈现非常接近全尺寸的内容。根据您的布局进行调整。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...