会议成为控制器方法无效 [英] session become null in controller method

查看:97
本文介绍了会议成为控制器方法无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下控制器,该控制器我创建的会话保存的IEnumerable 数据集

  [HttpPost]
    [ValidateInput(假)]
    公众的ActionResult Create_Brochure(IEnumerable的< ProductsPropertiesVM>模型)
    {        IEnumerable的< ProductsPropertiesVM> = newModel,并向模型;        IEnumerable的< BrochureTemplateProperties>样品= model.Where .....        会话[TemplateData] = newModel,并向;        返回查看(样品);
    }


编辑:

Create_Brchure查看页面有链接的href叫 PrintIndex 方法,同一个类文件

 < A HREF =@ Url.Action(PrintIndex,招生简章)>下载ViewAsPdf< / A>

这是 PrintIndex

 公众的ActionResult PrintIndex()
    {
        返回新Rotativa.ActionAsPdf(Create_Brochure_PDF){文件名=TestActionAsPdf.pdf};
    }


我想在 Create_Brochure_PDF 控制器方法再次使用会话列表数据集,所以我创建了这里的方法

 公众的ActionResult Create_Brochure_PDF()
    {
        IEnumerable的< ProductsPropertiesVM> = newModel,并向会话[TemplateData]为IEnumerable< ProductsPropertiesVM取代;        IEnumerable的< BrochureTemplateProperties> samplePDF = newmodel.Where(....        返回查看(samplePDF);
    }

但在上面的方法我越来越空的IEnumerable< ProductsPropertiesVM> newModel,并向

编辑:

如果我解释这整个场景


  1. Create_Brochure 控制器方法有一个观点,

  2. 在这个观点我HREF链接到 Create_Brochure 视图保存为
    PDF

  3. 当我点击链接的href我打电话 PrintIndex 方法,以便在
    这一行动再次方法调用它以 Create_Brochure_PDF 方法,
    所以我得到的 Create_Brochure_PDF 设置空对象


解决方案

我有同样的问题,有时前,所以我想出了解决办法 ViewasPdf()方法Rotativa库

您可以直接打电话到,一旦你点击链接的href,但你必须创建这个方法,你要生成PDF格式视图

所以这里的步骤


  1. 创建您要生成PDF格式的查看一个动作

     公众的ActionResult Create_Brochure_PDF()
    {    IEnumerable的< ProductsPropertiesVM> = newModel,并向会话[TemplateData]为IEnumerable< ProductsPropertiesVM取代;    IEnumerable的< BrochureTemplateProperties> samplePDF = newmodel.Where(....    退还或者拒不查看();

    }


  2. 生成视图,而且动作方法


  3. 替换该行退还或者拒不查看(); Create_Brochure_PDF以下行()


返回新Rotativa.ViewAsPdf(Create_Brochure_PDF){文件名=TestActionAsPdf.pdf};

<醇开始=4>
  • 现在,调用 Create_Brochure_PDF()方法如下中
    Create_Brochure视图页

  • &LT; A HREF =@ Url.Action(Create_Brochure_PDF,招生简章)&GT;下载ViewAsPdf&LT; / A&GT;

    I have following controller , in that controller I created session to save IENUMERABLE data-set

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create_Brochure(IEnumerable<ProductsPropertiesVM> model)
        {
    
            IEnumerable<ProductsPropertiesVM> newmodel = model;
    
            IEnumerable<BrochureTemplateProperties> sample = model.Where.....
    
            Session["TemplateData"] = newmodel;
    
            return View(sample);
        }
    


    EDIT:

    Create_Brchure View page has href link to call PrintIndex method in same class file

    <a href="@Url.Action("PrintIndex", "Brochure")">Download ViewAsPdf</a>
    

    this is PrintIndex method

        public ActionResult PrintIndex()
        {
            return new Rotativa.ActionAsPdf("Create_Brochure_PDF") { FileName = "TestActionAsPdf.pdf" };
        }
    


    I want to use that session list dataset again in Create_Brochure_PDF controller method,so I created here that method

        public ActionResult Create_Brochure_PDF()
        {
            IEnumerable<ProductsPropertiesVM> newmodel = Session["TemplateData"] as IEnumerable<ProductsPropertiesVM>;
    
            IEnumerable<BrochureTemplateProperties> samplePDF = newmodel.Where(....
    
            return View(samplePDF);
        }
    

    but in above method I'm getting null IEnumerable<ProductsPropertiesVM> newmodel

    EDIT:

    If I explain this whole scenario

    1. Create_Brochure controller method has a view ,
    2. In that view I have href link to save that Create_Brochure view as PDF
    3. Once I click that href link I'm calling PrintIndex method so in that action method again its calling to Create_Brochure_PDF method , so I'm getting null object set in Create_Brochure_PDF

    解决方案

    I had the same issue some times ago, So I came up with solution ViewasPdf() method in Rotativa library

    You can directly call to once You click that href link but you have to create a View for this method that your going to generate as PDF

    So here the steps

    1. Create a Action for the View that your Going to Generate as PDF

      public ActionResult Create_Brochure_PDF()
      {
      
          IEnumerable<ProductsPropertiesVM> newmodel = Session["TemplateData"] as IEnumerable<ProductsPropertiesVM>;
      
          IEnumerable<BrochureTemplateProperties> samplePDF = newmodel.Where(.... 
      
          rerurn View();
      

      }

    2. Generate view for that Action method

    3. Replace this line rerurn View(); with following line in Create_Brochure_PDF() method

    return new Rotativa.ViewAsPdf("Create_Brochure_PDF") { FileName = "TestActionAsPdf.pdf" };

    1. Now call the Create_Brochure_PDF() method as follows in Create_Brochure view page

    <a href="@Url.Action("Create_Brochure_PDF", "Brochure")">Download ViewAsPdf</a>

    这篇关于会议成为控制器方法无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆