Michael Conklin
Michael Conklin

Reputation: 101

Getting the parent model ID when adding a child entry in MVC 3 w/razor?

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?

Upvotes: 0

Views: 1519

Answers (1)

mnsr
mnsr

Reputation: 12437

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>

Upvotes: 1

Related Questions