'EF4 generated CRUD views having problems with the Create due to parent-child table relationship

I am relatively new to .NET, and am building a website with MVC3, EF4, SQL 2008 R2. Also, I haven't found anything on Stack that directly seems to address my issue, which involves an EF-generated issue with a parent-child table relationship complication.

Our site has user profiles (table: Profile), which can each have several records on the Social table, which means that the Social table has an FK of ProfileId.

EF4 has generated CRUD views and controller for this Social table, and all works well save for the Create. The Create view auto-generated a drop-down box to allow the user to select which ProfileId they would like, but that's wrong. (I.e., we don't want someone to setup Social information for some other user, duh.) So I turned the drop-down into a @Html.HiddenFor(model => model.ProfileId) and at that point my problems began.

What happens is, when I click the CREATE button on that view, nothing happens. It seems very likely that this is because the page does not have the user's ProfileId in context, and I'm wondering what to change to overcome this hurdle. (The user's ProfileId is in context in the previous page, Index.cshtml, as I'm restricting the list of Social records to ones that match their Id.)

Some code from the Model:

using System;
using System.Collections.Generic;

public partial class Social
{
    public int SocialId { get; set; }
    public int ProfileId { get; set; }
    public Nullable<int> ThemeId { get; set; }
    public string Title { get; set; }
    public Nullable<System.DateTime> CreateDate { get; set; }
    public Nullable<System.DateTime> LastActivityDate { get; set; }
    public Nullable<bool> Active { get; set; }
    public Nullable<bool> Publish { get; set; }

    public virtual Profile Profile { get; set; }
    public virtual Theme Theme { get; set; }
}

Controller code:

    public ActionResult Create()
    {
        ViewBag.ProfileId = new SelectList(db.Profiles, "ProfileId", "Profile");
        ViewBag.ThemeId = new SelectList(db.Themes, "ThemeId", "ThemeTitle");
        return View();
    } 

    [HttpPost]
    public ActionResult Create(Social social)
    {
        if (ModelState.IsValid)
        {
            db.Socials.Add(social);
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        ViewBag.ProfileId = new SelectList(db.Profiles, "ProfileId", "Profile", social.ProfileId);
        ViewBag.ThemeId = new SelectList(db.Themes, "ThemeId", "ThemeTitle", social.ThemeId);
        return View(social);
    }

...and from the View:

    <div class="editor-label">
        @Html.LabelFor(model => model.ProfileId, "Profile")
    </div>
    <div class="editor-field">
        @Html.DropDownList("ProfileId", String.Empty)
        @Html.ValidationMessageFor(model => model.ProfileId)
    </div>

Any help would be greatly appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source