0

I'm getting IEnumerable<object> list to Create_Brochure controller method as follows

enter image description here

So I want to keep this object list and reuse in Create_Brochure_PDF controller method

So I added following lines to create session in Create_Brochure controller method

IEnumerable<ProductsPropertiesVM> newmodel = model;

Session["TemplateData"] = newmodel;

to Use that session in Create_Brochure_PDF controller method I added following lines to Create_Brochure_PDF controller method

IEnumerable<ProductsPropertiesVM> newmodel = Session["TemplateData"] as IEnumerable<ProductsPropertiesVM>;

but I'm getting null for this newmodel in Create_Brochure_PDF controller method

I need to know how can I define a Session for IEnumerable<object> list and reuse it in another method correctly.

kez
  • 2,273
  • 9
  • 64
  • 123
  • 1
    Try like this `IEnumerable newmodel =(IEnumerable)Session["TemplateData"];` – Waqar Ahmed Nov 06 '15 at 04:10
  • Same thing, except you can get an InvalidCastException if the cast fails rather than null. – Eric J. Nov 06 '15 at 04:11
  • @EricJ. Once I debug I can see its `null` not a cast issue i think – kez Nov 06 '15 at 04:12
  • @kez: If your cast is invalid using the `as` syntax, the expression produces null. – Eric J. Nov 06 '15 at 04:13
  • @EricJ. getting this kind compile time error once I try your approach `'object' does not contain a definition for 'Cast' and the best extension method overload 'Queryable.Cast(IQueryable)' requires a receiver of type 'IQueryable'` – kez Nov 06 '15 at 04:15
  • Can you please edit your post and clarify if you are losing object altoghether (`Session["TemplateData"]` is null) or it has wrong type (show result of `Session["TemplateData"].GetType().FullName)` than) – Alexei Levenkov Nov 06 '15 at 04:15
  • @WaqarAhmed where should I replace that – kez Nov 06 '15 at 04:16
  • Where you are casting the session back to `newmodel`. – Waqar Ahmed Nov 06 '15 at 04:17
  • @AlexeiLevenkov I cannot understand what you said here this is relevant code snippet https://bitbucket.org/snippets/Common_Admin/rLXR5 – kez Nov 06 '15 at 04:20
  • check session timeout in web.config. or You can add it in . – Ankush Jain Nov 06 '15 at 05:19
  • @AnkushJain added like this but not working ` ` – kez Nov 06 '15 at 05:33
  • add debug point for both Create_Brochure_PDF() controller method and PrintIndex() and find out where its going to be null – Kelum Nov 08 '15 at 10:29
  • What happens when you use `Session["TemplateData"] = newmodel.ToList();`, to store a real list in session instead of a Queryable that maybe needs a live database connection? – Hans Kesting Nov 08 '15 at 11:08

2 Answers2

0

Try like this:

IEnumerable<ProductsPropertiesVM> newmodel =(IEnumerable<ProductsPropertiesVM>)Session["TemplateData"];

Because If your cast is invalid using the as syntax, the expression produces null.

[Reference#1] [Reference#2]

Community
  • 1
  • 1
Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35
0

I think there is no problem of your definition ,

add your following line inside PrintIndex() , Create_Brochure_PDF() methods and add debug point for both Create_Brochure_PDF() controller method and PrintIndex() and find out where its going to be null

IEnumerable<ProductsPropertiesVM> newmodel = Session["TemplateData"] as IEnumerable<ProductsPropertiesVM>;
Kelum
  • 1,745
  • 5
  • 24
  • 45