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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:12:06+00:00 2026-05-27T21:12:06+00:00

I’ve spent the last 2 days trying to figure this out, basically I have

  • 0

I’ve spent the last 2 days trying to figure this out, basically I have two Models (Event and EventStyle), but EventStyle one will not bind no matter what I try.

These classes are part of a Code-First database, and the Event model has a foreign key to EventStyle.

Here are my watered-down models:

public class Event {
    public string Id { get; set; }
    public string Name { get; set; }     
    public string Description { get; set; }
    public virtual EventStyle Style { get; set; }
}

public class EventStyle {
    public string Id { get; set; }
    public string Image { get; set; }
}   

In my controller I have this:

[HttpPost]
public ActionResult Create(Event evt) { /* add evt to the database */ }

And a simple form:

@using (Html.BeginForm()) {
    @Html.HiddenFor(evt => evt.Id)
    @Html.HiddenFor(evt => evt.Style)
    @Html.TextBoxFor(evt => evt.Name)
    @Html.TextAreaFor(evt => evt.Description)
}

(I actually have a custom @Html.EditorFor for evt.Style which changes the hidden field’s value)

When the form is submitted, the Event is correctly binded with Id, Name and Description.
However, the Style property remains null, even if the Hidden field contains a valid EventStyle Id in the database.

If I remove the hidden field, then Style becomes the default one (as set in Event’s constructor)

I also tried using a ModelBinder on EventStyle, but the correct Id never comes through the bindingContext, this might be part of the problem.

However, the correct Id does come through the Binder controllerContext, or directly using a FormCollection in the Controller. I’d much rather get ModelBinding to work properly though.

Maybe the ModelBinder doesn’t have any knowledge of my database? If so, how could I get it to recognize my database?

EDIT: hmm just removed the virtual and now the Binder is picking up the correct Id from the form, but it’s still not getting to the Event model

EDIT2: solved, using this to load the EventStyle from the database:

if (evt.Style != null) {
    evt.Style = db.EventStyles.Find(evt.Style.Id);
}
  • 1 1 Answer
  • 1 View
  • 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-27T21:12:06+00:00Added an answer on May 27, 2026 at 9:12 pm

    Use the Id property itself:

    @Html.HiddenFor(evt => evt.Style.Id)
    

    Edit

    Something to note is that @html.[InputType]For() helper methods are there to set the appropriate name/id attributes on the elements in the markup so that when they are posted, the default model binder will know how to set the properties on your model.

    If you look at the html markup, you will see your style element is created like this:

    <input id="Style_Id" type="hidden" value="" name="Style.Id" />
    

    This is the naming convention that the default model binder understands and what it uses to set the properties on your model.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.