Simple: retrieve the dictionary from the session; if it's not null, try to retrieve the value from the dictionary.
var values = (Dictionary<string, object>)Session["sess_values"];
if (values != null)
{
object value;
if (values.TryGetValue("name", out value))
{
string s = value as string;
if (s != null)
{
model.Name = s;
}
}
}
If you're using a recent version of Visual Studio, you can simplify that slightly:
var values = (Dictionary<string, object>)Session["sess_values"];
if (values?.TryGetValue("name", out object value) && value is string s)
{
model.Name = s;
}
But it's not obvious why you're using a
Dictionary<TKey, TValue>
in the first place, when you could just use the session directly:
Session["name"] = model.Name;
model.Name = (string)Session["name"];