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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:24:58+00:00 2026-05-29T08:24:58+00:00

So in a one to many relational database structured app, you of course need

  • 0

So in a one to many relational database structured app, you of course need to be able to add multiple child entries for a parent… So let’s say that you have a User, and the User can have one Profile, but each profile can have multiple Projects, and of course Projects consist of multiple child tables, such as for example, ProjectCodeSamples, being one of them…

So obviously to add a new ProjectCodeSample, it requires the ProjectID that it belongs to (as the foreign key)… So in the AddCodeSample View, how do you get that value, the value of the ProjectID, so that you can put it in a hidden field and pass it?

Here is the ProjectCodeSample Model:

namespace ProDevPortMVC3
{
    using System;
    using System.Collections.Generic;

    public partial class ProjectCodeSample
    {
        public int ProjectCodeSampleID { get; set; }
        public int ProjectID { get; set; }
        public string ProjectCodeSampleContent { get; set; }
        public string ProjectCodeSampleDescription { get; set; }

        public virtual Project Project { get; set; }
    }
}

Here are my two Controller Action Methods for the AddCodeSample:

//
// GET: /Project/AddCodeSample

public ActionResult AddCodeSample()
{
    return View();
}

//
// POST: /Project/AddCodeSample

[HttpPost]
public ActionResult AddCodeSample(ProjectCodeSample projectcodesample)
{
    projectcodesample.ProjectCodeSampleContent = projectcodesample.ProjectCodeSampleContent.TrimStart();

    if (ModelState.IsValid)
    {
        db.ProjectCodeSamples.Add(projectcodesample);
        db.SaveChanges();
        return RedirectToAction("Details/" + projectcodesample.ProjectID);
    }
    else
    {
        return View(projectcodesample);
    }
}

Here is my AddCodeSample View:

@model ProDevPortMVC3.ProjectCodeSample

@{
    ViewBag.Title = "Add Code Sample";
}

<style type="text/css">
    .editor-label { width: 200px; }
    .editor-field { width: 700px; }
    .submit-button-row { width: 900px; }
    .bottom-nav { width: 900px; }
</style>

<h2>Add Code Sample</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset style="float: left; width: 900px;">
        <legend>Project Code Sample</legend>
        @Html.HiddenFor(model => model.ProjectID)
        <div class="display-editor-label-field-row">
            <div class="editor-label">
                @Html.LabelFor(model => model.ProjectCodeSampleDescription, "Code Sample Description")
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model.ProjectCodeSampleDescription, 4, 85, null)
                @Html.ValidationMessageFor(model => model.ProjectCodeSampleDescription)
            </div>
        </div>
        <br />
        <div class="display-editor-label-field-row">
            <div class="editor-label">
                @Html.LabelFor(model => model.ProjectCodeSampleContent, "Code Sample")
            </div>
            <div class="editor-field">
                @Html.TextAreaFor(model => model.ProjectCodeSampleContent, 15, 85, null)
                @Html.ValidationMessageFor(model => model.ProjectCodeSampleContent)
            </div>
        </div>
        <br />
        <div class="display-editor-label-field-row">
            <div class="submit-button-row">
                <input type="submit" value="Save" />
            </div>
        </div>
        <br />
        <div class="bottom-nav">
            @Html.ActionLink("Cancel", "Details", new { id = ViewContext.RouteData.Values["ID"] })
        </div>
    </fieldset>
}

So obviously this will not work, because “@Html.HiddenFor(model => model.ProjectID)” will be empty… So what am I doing wrong, how do you get the Parent ID so that you can make Child entries?

  • 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-29T08:24:59+00:00Added an answer on May 29, 2026 at 8:24 am

    You could pass the id from the previous page into this page using a querystring in your link:

    public ActionResult AddCodeSample(int id)
    {
        var m = new ProjectCodeSample();
        m.ProjectID = id;
        return View(m);
    }
    

    So on your ‘parent’ project page, it could have a link like this:

    <a href="/addcodesample?id=2">Add Code Sample</a>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple level parent-child tables(GrandParent, Parent and Child, all many to one relation,
I have a question about Criteria method, one-to-many relation to the database, 'one' is
I need to store data that looks like this: <root> <child-one> value 1 value
Greetings Overflowers, I need to query against objects with many/complex spacial conditions. In relational
Let's say I have a relational database with tables: OrderableCategories and Orderables. They are
I want to create a view in a one-to-many relation. Here are my relations:
When using generateModelsFromDb to generate the models Doctrine makes one to many relations between
I was reading about inverse = true in one-to-many relation and was actually wondering
I am having a user object which has one-to-many relation with Address object. My
I'm looking for a solution to manage a one-to-many relation within an HTML form

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.