First a quick question, do I have to use fluent API to map BaseEntity Fields which a model inherited?
Now my problem is I have a model which inherits a Forign Key and Navigation Property, the Model.IsValid is returning false saying the property is emplty when being submitted to controller, even if I put a foriegn key in the field the navigation property
still says false, and I have no idea why.
The error in the debugger is:
Exception = {"The parameter conversion from type 'System.String' to type failed because no type converter can convert between these types.
First a quick question, do I have to use fluent API to map BaseEntity Fields which a model inherited?
What do you mean by the "fluent API" ? You could create a custom model binder, it's not very difficult.
You shouldn't be exposing your entities with navigation fields to the view and model binder. These problems go away if you just create a dedicated view model specifically tailored to your view (including projection/flattening). Then just create an object
that uses automapper or to map data back to the entity object.
However this is a crud page for say a product, and the navigation property is to brand, for something as simple as that I could use a flatterner view model.
However some of my crud pages and models get more complicate, and and even if I had a view model with would still have navigation properties, won't their still be the same issue?
However some of my crud pages and models get more complicate, and and even if I had a view model with would still have navigation properties, won't their still be the same issue?
In that case the view model would encompass the fields you need, including those in related objects. Then you just map it back to the entity object either using auto mapper or manually.
EnenDaveyBoy
Participant
1465 Points
1144 Posts
model.IsValid Issue with navigation property
Apr 03, 2012 09:57 PM|LINK
Hi
First a quick question, do I have to use fluent API to map BaseEntity Fields which a model inherited?
Now my problem is I have a model which inherits a Forign Key and Navigation Property, the Model.IsValid is returning false saying the property is emplty when being submitted to controller, even if I put a foriegn key in the field the navigation property still says false, and I have no idea why.
The error in the debugger is:
Exception = {"The parameter conversion from type 'System.String' to type failed because no type converter can convert between these types.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: model.IsValid Issue with navigation property
Apr 03, 2012 10:07 PM|LINK
What do you mean by the "fluent API" ? You could create a custom model binder, it's not very difficult.
You shouldn't be exposing your entities with navigation fields to the view and model binder. These problems go away if you just create a dedicated view model specifically tailored to your view (including projection/flattening). Then just create an object that uses automapper or to map data back to the entity object.
Blog | Twitter : @Hattan
EnenDaveyBoy
Participant
1465 Points
1144 Posts
Re: model.IsValid Issue with navigation property
Apr 03, 2012 10:23 PM|LINK
I will look into custom model binders.
However this is a crud page for say a product, and the navigation property is to brand, for something as simple as that I could use a flatterner view model.
However some of my crud pages and models get more complicate, and and even if I had a view model with would still have navigation properties, won't their still be the same issue?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: model.IsValid Issue with navigation property
Apr 03, 2012 10:27 PM|LINK
In that case the view model would encompass the fields you need, including those in related objects. Then you just map it back to the entity object either using auto mapper or manually.
Blog | Twitter : @Hattan
EnenDaveyBoy
Participant
1465 Points
1144 Posts
Re: model.IsValid Issue with navigation property
Apr 03, 2012 10:36 PM|LINK
I was hoping to avoid viewModels like this, until I got to the front end, as i wanted to avoid another layer at this stage.
I guess i will have to add some more models to my project.
I had redesigned my project so I thought it would make it cleaner, but its now becoming massive lol
EnenDaveyBoy
Participant
1465 Points
1144 Posts
Re: model.IsValid Issue with navigation property
Apr 03, 2012 10:37 PM|LINK
forgot to say thanks, Thanks