会话在控制器方法中变为空

时间:2022-09-25 14:42:55

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

我有一个控制器,在那个控制器中我创建了一个session来保存IENUMERABLE数据集。

    [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

Create_Brchure视图页面具有href链接,可以在同一个类文件中调用PrintIndex方法

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

this is PrintIndex method

这是PrintIndex方法

    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

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

    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

但是在上面的方法中,我得到了空的IEnumerable 新模型

EDIT:

编辑:

If I explain this whole scenario

如果我解释整个场景

  1. Create_Brochure controller method has a view ,
  2. create_小册子控制器方法有一个视图,
  3. In that view I have href link to save that Create_Brochure view as PDF
  4. 在这个视图中,我有href链接来将create_handbook视图保存为PDF
  5. 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
  6. 一旦我点击了href链接,我就调用了PrintIndex方法在那个动作方法中,它再次调用了Create_Brochure_PDF方法,所以我在Create_Brochure_PDF中获得了空对象集

1 个解决方案

#1


1  

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

我以前也遇到过同样的问题,所以我在Rotativa库中提出了解决方案ViewasPdf()方法

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

您可以直接调用,一旦您点击了href链接,但您必须为这个方法创建一个视图,您将生成PDF

So here the steps

这里的步骤

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

    为要生成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

    替换这个行rerurn视图();在Create_Brochure_PDF()方法中使用以下行

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

返回新的Rotativa.ViewAsPdf(“Create_Brochure_PDF”){文件名=“TestActionAsPdf”。pdf " };

  1. Now call the Create_Brochure_PDF() method as follows in Create_Brochure view page
  2. 现在调用Create_Brochure_PDF()方法,如下面的create_handbook视图页面所示

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

< a href = "。行动(“Create_Brochure_PDF”、“小册子”)”>下载ViewAsPdf < / >

#1


1  

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

我以前也遇到过同样的问题,所以我在Rotativa库中提出了解决方案ViewasPdf()方法

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

您可以直接调用,一旦您点击了href链接,但您必须为这个方法创建一个视图,您将生成PDF

So here the steps

这里的步骤

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

    为要生成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

    替换这个行rerurn视图();在Create_Brochure_PDF()方法中使用以下行

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

返回新的Rotativa.ViewAsPdf(“Create_Brochure_PDF”){文件名=“TestActionAsPdf”。pdf " };

  1. Now call the Create_Brochure_PDF() method as follows in Create_Brochure view page
  2. 现在调用Create_Brochure_PDF()方法,如下面的create_handbook视图页面所示

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

< a href = "。行动(“Create_Brochure_PDF”、“小册子”)”>下载ViewAsPdf < / >