Using EF with MVC4 allows you to specify the inclusion of Foreign Key columns in the model. While this not normally part of OR modeling, it does allow MVC4 to automatically generate views with dropdown lists, for the foreign key relationships, when you generate a controller with the MVC controller with read/write actions and views, using Entity Framework
option.
I have hit a problem creating an object in this scenario.
Greatly simplified, the models in question are:
Questionnaire:
- QuestionnaireID: PK
- CandidateId: FK
- Candidate: Associated object
Candidate:
- CandidateID: PK
- Name: string
The problem I have hit is that on a Create view post-back to create a new Questionnaire ModelState.IsValid
is false. On investigation the error listed is The parameter conversion from type 'System.String' to type 'Data.Candidate' failed because no type converter can convert between these types.
ModelState.Keys includes Questionnaire.Candidate
as well as Questionnaire.CandidateId
(which is valid).
I am sure this is something simple, but would like to hear some solutions. The viewbag only has a set for the drop-down list and the view has an @model of type @model Data.Questionnaire
. There are no editor fields bound to Questionnaire.Candidate.
.