Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6955501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:44:40+00:00 2026-05-27T14:44:40+00:00

I have a model class called House: public class House { public House() {

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:44:41+00:00Added an answer on May 27, 2026 at 2:44 pm

    Although it stil uses the Session behind the scene, TempData is the way to go when you want to persist data across actions calls.

    An action method can store data in the controller’s TempDataDictionary object before it calls the controller’s RedirectToAction method to invoke the next action. The TempData property value is stored in session state.

    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:

    public ActionResult AddResident(Resident resident)
    {
        IList<Resident> residents = PeristResident(resident);
        return PartialView("_Residents", residents);
    }
    
    private IList<Resident> PeristResident(Resident resident)
    {
        IList<Resident> residents = Residents; // this operation can empty TempData by reading it
        residents.Add(resident);
        Residents = residents; // so we persists collection after read
        return residents;
    }
    
    private IList<Resident> Residents
    {
        get
        {
            object results;
            if (TempData.TryGetValue("Residents", out results) == false)
            {
                var list = new List<Resident>();
                return list;
            }
            return (IList<Resident>)results;
    
        }
        set { TempData["Residents"] = value; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model object called Country : class Country { public string Name
I have a model that has an array of Strings called tags, public class
I have a class called MODEL in which public static int theMaxFrames resides. The
I have a model class called VideoGame . I need the class to get
Assume I have a Model class called Bird and a instance of Bird called
Let's say I have a model class called Project, but instead of this: class
I have a model called Contact which has_one guest like so. class Contact <
I have a model called company that has_many users then users belongs_to company. class
I have a class in my domain model called JobPlan this class is stored
I have a model class Action that get's extended by several other classes. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.