如何将pdf从一个控制器显示到另一个控制器

时间:2016-02-18 19:26:17

标签: c# asp.net-mvc

我有一个由一个控制器呈现的pdf列表。

现在,当我点击编辑按钮时,它应该在iframe中显示pdf。我能够做到这一点,但它只显示第一个pdf而不是另一个。

尝试使用tempdata但不工作id不会从一个控制器传递到另一个控制器。

 public IServiceProvider ConfigureServices(IServiceCollection services)
 {
     // We add MVC here instead of in ConfigureServices.
     services.AddMvc();

     // Create the Autofac container builder.
     var builder = new ContainerBuilder();

     // Add any Autofac modules or registrations.
     builder.RegisterModule(new AutofacModule());

     // Populate the services.
     builder.Populate(services);

     // Build the container.
     var container = builder.Build();

     // Resolve and return the service provider.
     return container.Resolve<IServiceProvider>();
 }

1 个答案:

答案 0 :(得分:1)

因为您在集合上调用First()方法。

您应该做的是,传递唯一的文件ID并使用该ID查询您的集合并返回该内容。

public ActionResult GetFile(int id)
{
   var f= obj.GetFiles.FirstOrDefault(s=>s.FileId ==id);
   if(f!=null)
   {
      // return the file stream
   }
   // return something else
}

从编辑屏幕调用时,请确保传递文件的唯一ID。

<iframe src="@Url.Action("GetFile","YourControllerName", new {id=14})" 
                                            style="width:718px; height:700px;" ></iframe> 

14替换为有效的唯一文件ID(可能是视图模型的属性值)