The Goal
Encode a List<T>Session["MySession"]
to JSON with Razor Engine.
The Problem
I have the following script in my Index.cshtml
:
<script type="text/javascript">
var existingItems =
@Html.Raw
(Json.Encode
((List<MyApp.Models.Data.getSpecificProductToShoppingList_Result>)
Session["ProdructsSummary"]))
window.onload = function () {
console.log(existingItems);
}
</script>
And the return of existingItems
is null
. Why?
Observation
I'm sure — there is a value on my Session.
You don't need to cast to the underlying type. You could simply write:
Now you are saying that this generates the following markup:
What about this:
Can you see the alert? I am sure you can see it. I am even ready to bet 5 bucks on it assuming your statement about getting null is true.
Conclusion:
Session["ProdructsSummary"]
is null so don't expect thatJson.Encode
will return something else.