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

  • Home
  • SEARCH
  • 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 1072291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:50:10+00:00 2026-05-16T20:50:10+00:00

I have a wizard in my asp.net MVC application which is built upon this

  • 0

I have a wizard in my asp.net MVC application which is built upon this SoG – RESTFUL wizard guide.

In order to understand the problem I will first explain the situation below. For this I will use a dummy situation, we try to create a person with an address. Again, this is just a dummy situation!

The situation

At start, the object (person) to be created and saved using the wizard is created as a blank object (person) and will be filled during the wizard ‘stage’. In between the various wizard steps the person object is stored in the SESSION and retrieved with a key. This object has a relation with a sub-object. For example, an address. This address can either be retrieved from a DB using a dropdown-menu in the wizard or can be created in the wizard itself. This means that at creation I will create an empty address-object so we have the following initialization:

Persons p = new Persons();
p.Addressess = new Addressess()

This object is passed to the FormViewModel and used in the Wizard Form. Here I have a drop-down enabling to choose: (1) *create new address for person”, which fills the passed empty-address object with data using the usual ways (TryUpdateModel()) and (2) “address x”, addresses to select from your DB.

Selecting one of the addresses in the dropdown will, in the controller POST method, retrieve this object from DB and couple it to the person.Addresses. In code:

p.Addresses = repository.GetContactByID(id);

The problem

Everything works fine while running through the wizard pages. The problem occurs when I call the savechanges(). While in a final overview the complete object to be added is shown correctly (person info + address info as selected / passed), also an empty address is saved to the database.

repository.SaveChanges();

This will try to add an empty Addresses to the DB which will introduce a ModelState error since Addresses have some non-nullable’s which are not set for the empty object.

** My current idea **
My current thinking is that the empty object created at start for the blank object is somehow placed in added state (objectcontext) when I first couple it to person.Addresses. Can this be the case? What would be the correct way to do what I want? Can I manually delete things from the object context _addedEntityStore ?

Additional Info

Selection of an address in the dropdown will force a form.submit to the POST controller method and consecutively it will reload the form with updated selection info and the input fields for the new address (in case a new one is wanted) set to “disabled” so you only see the info but cannot edit an existing address.

Also, only one objectContext is used which is saved in the SESSION.

  • 1 1 Answer
  • 1 View
  • 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-16T20:50:10+00:00Added an answer on May 16, 2026 at 8:50 pm

    Although Hightechrider is right about using an object context for every unit of work I post the simple solution I used for now. When you somehow (forced, chosen, whatever) have your object context stored in SESSION you can solve the above problem simply by calling:

    repository.Detach(person.Addresses) 
    

    Which will detach the old object from the context (not saved yet to DB). And after detaching, attach the new object you want to have it linked to.

    person.Addresses = repository.GetAddressByID(id);
    // or
    person.Addresses = new Addresses();
    

    Though I do recommend rewriting your wizard if you need to do this and have the time and power :).

    edit

    note that Detach only detaches the object supplied, not its related objects (!).

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

Sidebar

Related Questions

I'm building a wizard-style application in ASP.NET MVC and is wondering if you have
I have this ASP.net MVC project that requires a wizard like interface... So I
i build a small application with asp.net mvc framework. I have one question. I
I have an ASP.NET MVC application that uses LINQ2SQL as the database layer. I
I have an MVC web application in asp.net 3.5 How can I convert that
I have requirement to create an asp.net wizard control which will be re usable
I just joined a team thats developing a asp.net mvc 1 application. I only
I've been experimenting with ASP.Net MVC, and have come across a problem that is
Coding Platform: ASP.NET C# Controls Used: asp:Wizard and asp:Button I have a asp:Wizard and
I use MVC2/asp.net and try to develop something like a wizard. This wizard will

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.