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);
}
Use the Id property itself:
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:
This is the naming convention that the default model binder understands and what it uses to set the properties on your model.