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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:01:28+00:00 2026-06-07T19:01:28+00:00

I’m trying to get my head around related data for use with a master

  • 0

I’m trying to get my head around related data for use with a master detail scenario using EF in MVC. Searching StackOverflow got me on the road for a good part, but I’m running into problems while saving my data. As more examples on StackOverflow i use a person/address set up, and for the person as well as the address (which is one-to-many) i only use part of the entities properties on my views. With the default scaffold this results in an error, but for the person entity i managed to solve that using the the code below. But for the Address entity I now get the same error and i don’t know how to solve this. Here’s what i’ve got so far:

The ContactController:

public ActionResult Edit(int id)
{
    Contact contact = db.Contacts
        .Include(c => c.Address)
        .Include(c => c.Relation)
        .Where(c => c.ContactId == id)
        .Single();
    return View(contact);
}


[HttpPost]
public ActionResult Edit(int id, FormCollection formCollection)
{
    var contactToUpdate = db.Contacts
        .Include(c => c.Address)
        .Include(c => c.Relation)
        .Where(c => c.ContactId == id)
        .Single();

    if (TryUpdateModel(contactToUpdate, "", null, new string[] { "Relation" }))
    {
        try
        {
            db.Entry(contactToUpdate).State = EntityState.Modified;
            db.SaveChanges();

            return RedirectToAction("Index");

        }
        catch (DataException ex)
        {
            ModelState.AddModelError("", "Error while saving.");
            return View();
        }
    }
    return View(contactToUpdate); 
}

The classes for my models:

namespace MyApp.Models
{
    public class Contact
    {
        public int ContactId { get; set; }
        public string Weergavenaam { get; set; }

        public virtual ICollection<Address> Address{ get; set; }
        public virtual ICollection<Relation> Relation { get; set; }
    }

    public class Address
    {
        public int AddressId { get; set; }
        public string Street{ get; set; }
        public string Postal{ get; set; }
        public string Place{ get; set; }
        public string State{ get; set; }

        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }

        public virtual Contact Contact { get; set; }
    }

    public class Relation
    {
        ...
    }
}

The view for the ContactController’s Edit action:

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Contact</legend>

    @Html.HiddenFor(model => model.ContactId)

    <div class="editor-label">
        @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Name)
        @Html.ValidationMessageFor(model => model.Name)
    </div>

    @Html.EditorFor(model => model.Address) 


    <div class="display-field">
        Created: @Html.DisplayFor(model => model.Created)
    </div>
    <div class="display-field">
        Modified: @Html.DisplayFor(model => model.Modiied)
    </div>

    <p><input type="submit" value="Save" /></p>
</fieldset>
}

The EditorTemplate for the Address model

@model MyApp.Models.Address
<fieldset>
    <legend>Address</legend>

    @Html.HiddenFor(model => model.AddressId)

    <div class="editor-label">
        @Html.LabelFor(model => model.Street)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Street)
        @Html.ValidationMessageFor(model => model.Street)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Postal)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Postal)
        @Html.ValidationMessageFor(model => model.Postal)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.Place)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.Place)
        @Html.ValidationMessageFor(model => model.Place)
    </div>

    <div class="editor-label">
        @Html.LabelFor(model => model.State)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.State)
        @Html.ValidationMessageFor(model => model.State)
    </div>
</fieldset>
  • 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-07T19:01:30+00:00Added an answer on June 7, 2026 at 7:01 pm

    After struggling a bit with the code I figured that for now the only way to solve the problem was to add the missing properties to the editor template using @Html.HiddenFor.
    Of course I’m sure there should be a more ellegant way to get my situation working, but for now it solves my problem.

    Any better solution to the issue is welcome!

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported

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.