会议成为控制器方法无效 [英] session become null in controller method
问题描述
我有如下控制器,该控制器我创建的会话保存的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,并向
编辑:
如果我解释这整个场景
-
Create_Brochure
控制器方法有一个观点, - 在这个观点我HREF链接到
Create_Brochure
视图保存为PDF
- 当我点击链接的href我打电话
PrintIndex
方法,以便在
这一行动再次方法调用它以Create_Brochure_PDF
方法,
所以我得到的Create_Brochure_PDF
设置空对象
我有同样的问题,有时前,所以我想出了解决办法 ViewasPdf()
方法Rotativa库
您可以直接打电话到,一旦你点击链接的href,但你必须创建这个方法,你要生成PDF格式视图
所以这里的步骤
-
创建您要生成PDF格式的查看一个动作
公众的ActionResult Create_Brochure_PDF()
{ IEnumerable的< ProductsPropertiesVM> = newModel,并向会话[TemplateData]为IEnumerable< ProductsPropertiesVM取代; IEnumerable的< BrochureTemplateProperties> samplePDF = newmodel.Where(.... 退还或者拒不查看();}
-
生成视图,而且动作方法
-
替换该行
退还或者拒不查看();
与Create_Brochure_PDF以下行()
法
返回新Rotativa.ViewAsPdf(Create_Brochure_PDF){文件名=TestActionAsPdf.pdf};
Create_Brochure_PDF()
方法如下中Create_Brochure视图页
< A HREF =@ Url.Action(Create_Brochure_PDF,招生简章)>下载ViewAsPdf< / A>
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
Create_Brochure
controller method has a view ,- In that view I have href link to save that
Create_Brochure
view asPDF
- Once I click that href link I'm calling
PrintIndex
method so in that action method again its calling toCreate_Brochure_PDF
method , so I'm getting null object set inCreate_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
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();
}
Generate view for that Action method
Replace this line
rerurn View();
with following line inCreate_Brochure_PDF()
method
return new Rotativa.ViewAsPdf("Create_Brochure_PDF") { FileName = "TestActionAsPdf.pdf" };
- 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屋!