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 7019747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:13:33+00:00 2026-05-27T23:13:33+00:00

Im persisting my data in MVC3 and have come across an annoying problem: Lets

  • 0

Im persisting my data in MVC3 and have come across an annoying problem:

Lets say I have MyModel a = new MyModel() & MyModel b = new MySubmodel() defined in my constructor.

When I visit page 1:

 if (MyModel.MySubmodel== null)
  {
    //populate with defaults
  }else{
     //use existing model data
  }

On page 1, I can change the values using Html.textboxFor fields. I have my model updating with:

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Page1(Mysubmodel model)
        {

            MyModel.Mysubmodel = model;
            return RedirectToAction("Page2", "Tool");
        }

using a watch ,I can see that my model is updating with the new values! (which is great!), however, If i try to return to my page to check if the results are saved they are set to null again

Code:

 public class MyController : Controller
{
  //set up initial models
       public MyModel mainmodel = new MyModel ();
       public MySubModel submodel = new MySubModel ();
 public ActionResult Page1()
        {
            tempList.Clear();
            service.XmlParseDefault(Request); //Acquire defaults
            //setup model 
            //could possibly throw this into .services if it becomes a "fat controller"
            if (mymodel.submobdel == null)
            {
                //Apply default values
            }
        return View(submodel);
}

 [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Page1(submodel model)
        {

            mymodel.submodel = model;
            return RedirectToAction("Page2", "homecontroller");
        }


}
  • 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-27T23:13:33+00:00Added an answer on May 27, 2026 at 11:13 pm

    HTTP : request/response protocol

    Once a request is fulfilled by the Server by rendering the appropriate response (can be html, xml or json) to the Client, everything is flushed out of the memory. (Exceptions to this are session states).

    Therefore, in your case, when a request is received, the two model instances are created in the constructor and destroyed after the controller class goes out of scope i.e. it renders the view. So, when your form is submitted, the instances for the models are again created and that’s why they always show null values.

    What if i used ViewData in my postback to store the MyModel? Would I be right in thinking that this would store the entire model(with submodels), then read it in in the constructor of the page. Also I assume that unlike sessions, ViewData is autodestroyed after one use?

    Don’t user ViewData, always use model and pass them to the views. Retrieve them when you post back. Assign the postback model to new instances of models and save the changes somewhere persistent. Like in a database or xml file.

    Using a Static class can help you out, but it’s not recommended. Because as i said, these classes live as long as the application recycles. Use a database or xml file. Even for testing purposes

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem with persisting data via play-framework. Maybe it's not possible to
I have a class of data that I am persisting using JDO on GWT
Will declaring a dataset as STATIC in ASP.net website help in persisting data across
If you have been successful in persisting your data, which type of stream did
I have the following data model class defined: @PersistenceCapable public class TestSerializableModelObj { @Persistent(serialized=true,
I have some basic questions about core data (which I am new to) and
YAML seems like a great format for configuration files & data binding persistent objects
I have an application which stores data to the persistent store by setting the
I currently have the following objects persisting successfully: Person first name, etc. Exams title,
Is there any other alternative to SQLite in Android for persisting data in the

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.