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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:02:27+00:00 2026-06-06T07:02:27+00:00

I have 2 Models, each having many properties – Object1 and Object2 I have

  • 0

I have 2 Models, each having many properties – Object1 and Object2

I have foreign key relationship with them in SQL db – Object1 can have many Object2’s

Upon creating Object2 in my Object2Controller’s Create() Method, how would I associate Object2 with Object 1’s Guid?

Here is code:

Controller:

    [HttpPost]
public ActionResult Create(Object2 object2)
    {
        if (ModelState.IsValid)
        {
            object2.Object2Id = Guid.NewGuid();
            db.Object2.Add(object2);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }
        return View(object2);
    }
  • 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-06-06T07:02:30+00:00Added an answer on June 6, 2026 at 7:02 am

    I can only see two options in this scenario:

    1. Pre-fill the Object1 GUID in the model, when you post it back from the view, just like the other properties. This could be accomplished different ways depending on your requirements (user-selected drop-down, pre-populated into a hidden field, etc).
    2. Keep track on the server of which Object1 the current user is working on. You might do this using the Session object, or even using TempData.

    Edit

    To create a new Object2 from the Object1 detail view, follow this flow.

    First, a simple form on the detail view:

    @using (var form = Html.BeginForm("Add", "Object2Controller", FormMethod.Get)) 
    { 
        <input type='hidden' name='Object1ID' value='@Model.Object1ID' />
        <submit type='submit' value='Add Object1' />
    }
    

    Then you’ll have an Add method in the controller for Object2 that pre-populates the foreign key:

    [HttpGet]
    public ActionResult Add(Guid Object1ID)
    {
        Object2 newObj = new Object2();
        newObj.Object1ID = Object1ID;
        return View("MyAddView", newObj);
    }
    

    This would point to the Object2 detail form, which posts back to the Create method in your question.

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

Sidebar

Related Questions

I have three models: Customer, Bank and Account. each customer can have many accounts,
I have two models, Image and Tag . Each Image object can have more
I'm having trouble with my HABTM relationship in CakePHP. I have two models like
I have three models, each having the following associations: class Model1 < ActiveRecord::Base has_many
I have two models that are associated with each other. Customer has_one :primary_contact And
I have two models - Tenant and User, each tenant will have_many users and
I have a few models: 'Article, Video, BlogPost, News, Commodity'. Each are in their
I have a number of models that store words as a string with each
I have the following Django models: Bar, Bistro and Restaurant Each of the above
So I have an app with a 2 different models, Comments and Replies, each

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.