I have a model class called House:
public class House
{
public House()
{
Residents = new List<Resident>();
}
public virtual string Name { get; set; }
...
public virtual IList<Resident> Residents{ get; set; }
}
public class Resident
{
public virtual string Name { get; set; }
public virtual int Age{ get; set; }
public virtual House House { get; set; }
}
So, in my view Create (House), I need to add Residents . So I added a button “Add Resident”, that opens a JQuery UI Modal with Create(Resident) and when the user click Confirm, the Modal closes and my Resident´s grid refresh…
My problem here is where I save that list … I did that using Session… But I´d like to do that without Session…
What I´ve done (My House Controller):
[HttpGet]
public ActionResult AddResident(Resident resident) //Called when user confirms modal Resident
{
Residents.Add(resident);
return PartialView("_Residents", Residents);
}
public Collection<Resident> Residents
{
get
{
if (Session["Residents"] == null)
{
var _lista = new Collection<Resident>();
Session["Residents"] = _lista;
return _lista;
}
return (Collection<Resident>)Session["Residents"];
}
set { Session["Residents"] = value; }
}
So, what´s the right way to do that kind of scenario without session?
Thanks
Although it stil uses the
Sessionbehind the scene,TempDatais the way to go when you want to persist data across actions calls.The good part is that the session will be automatically cleared after your read (in your grid refresh method). It’s really what it means “temporary data”.
Here’s a sample of code with
TempData: