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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:34:38+00:00 2026-05-20T13:34:38+00:00

New to MVC so I’m sure there is a proper way to do this

  • 0

New to MVC so I’m sure there is a proper way to do this that I’m missing.

I have a Entity “Event” that has a child/related Entity “EventTime”. When I created my Edit Action I could load the Event model fine with a linq statement.

Event mEvent = db.Events.Single(s => s.EventID == id);

Then in my view I access the child by walking the object:

@Html.EditorFor(model => model.Event.EventTime.DateStart)

This works great. Edit Action gets the values, does the magic binding and saves changes.

UpdateModel(mEvent);  
db.SaveChanges();

Now to my Problem. The Create Action. In the sample Music Store app on asp.net they tell you to create a new empty model to pass to your view.

Event mEvent = new Event();

However my view gets an exception when it hits any reference to the child entity Event.EventTime. Also in the controller create action if I try to set any properties after creating the new mEvent object I get an Object reference exception.

Event.EventTime.DateStart = DateTime.Now;

I thought EF would magically create the child when you access it. I assume the issue is because I’m not creating the model from the db, it has no foreign key values to reference the objects together?

So how am I suppose to do this? I thought about a ViewModel and loading up each entity in that, then on my HTTPPost Action I assume I’d have to update the DB in lowest child first order and update the references as each one is saved to the database? Seems like it would work but feels like it’s defeating the purpose of EF.

Thanks for you help.

  • 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-20T13:34:39+00:00Added an answer on May 20, 2026 at 1:34 pm

    Morten, you are right. I had to create an instance of the EventTime object and assign it to Event.EventTime. That worked. I guess it makes sense it wouldn’t do it for me automatically. Thx for the comments.

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

Sidebar

Related Questions

I have a new MVC 2 site that has been working fine on my
New to MVC, so hopefully this will be pretty simple. I have an HTML
I've just created a new MVC 2 application that references both an entity model
I have a question that has pretty much been asked here: asp.net mvc Html.ActionLink()
I have this new mvc project where all beans are default scoped(no prototype or
I have been working on a new MVC application that utilizies EF4, POCO Domain
I have a new MVC project which uses the entity framework. I'm spitting out
Im using the New MVC Web Api I have something like this: public class
I have started a new mvc project and clicked on project\properties\web - use local
Pretty new to MVC and the like. I have a class the looks like

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.