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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:53:57+00:00 2026-05-25T15:53:57+00:00

I am new to .net mvc. I am trying to create a model with

  • 0

I am new to .net mvc. I am trying to create a model with a one to one relationship to another model.

I’d like to be able to display read only info on the edit pages for each model about its one to one relation.

Here is my code:

public class Thing1 : BaseEntity
{
    public virtual Thing2 thing2 { get; set; }
    [Required(ErrorMessage = "This field is required.")]
    public int Thing2ID { get; set; }

    ... Other basic fields go here
}

public class Thing2 : BaseEntity
{

    ...basic fields
}

The basic fields stand for just regular non-relational fields.

Now with this model, it is easy to display Thing2 data on the Thing1 form, but how do I display the Thing1 data on the Thing2 form? Is there something that I add to the Thing2 model to link it to thing1?

Background: I am using mvc3 with entity code first and razor.

  • 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-25T15:53:58+00:00Added an answer on May 25, 2026 at 3:53 pm

    Probably the easiest thing to do would be to add a new property to Thing2 for the relationship ,thus making it a bi-directional one-to-one relationship

    e.g.

    public class Thing2 : BaseEntity
    {
        public virtual Thing2 Thing2 { get; set; }
        ...basic fields
    }
    

    Then, you need to make sure that Thing 1 and Thing 2 don’t get out of sync. You could do this something like:

    public class Thing1 : BaseEntity
    {        
        private Thing2 thing2;
        public virtual Thing2 Thing2 { get { return thing2; }  }    
    
        public void SetThing2(Thing2 thing2)
        {
           this.thing2 = thing2;
           thing2.SetThing1(this);
    
        }
    
    }
    
    public class Thing2 : BaseEntity
    {
        private Thing1 thing1;
        public virtual Thing1 Thing1 { get { return thing1; }  }    
    
        public void SetThing1(Thing1 thing1)
        {
           this.thing1 = thing1;
           thing1.SetThing2(this);
        }
    }
    

    (Excuse typo’s, but you get the idea)

    That way, you can go Thing1.SetThing2(myThing2) (and vice versa) and both properties will be set.

    Without that kind of set-up (ie making the properties read-only, and making a convenience method to set both properties at once) you risk the objects getting out of sync with each other – so a Thing1 might reference a Thing2 without that Thing2 referencing Thing1.

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

Sidebar

Related Questions

I am trying to start a new project (asp.net MVC) and I would like
I'm only new to Unit Testing and ASP.NET MVC. I've been trying to get
I'm trying to add a new ADO.Net Entity Data Model to an MVC project
I'm new to ASP.NET MVC and I'm trying to create a very simple blog
I've been learning asp.net mvc 3. So, basically I'm trying to create a new
I trying to do something like that First, Create a new mvc 3 project
I am new to ASP.NET MVC 3. I am trying to create a basic
I have a new asp.net mvc project and i am trying to figure out
I'm trying to decide what validation approach to take for a new ASP.NET MVC
I'm new to asp.net mvc, but I'm trying to do an funny app with

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.