If using ViewModel, I was was always under the Impression that the View declared the ViewModel (passed by the GET ActionMethod) but when posting back, the POST ActionMethod declares the Model only and the Model Binder will handle the rest
I looked at your code and your POST action method needs to receiev an instance of your LedgerUserViewModel and not LedgerUser. The reason for this is because in your view the action helpers are configured to generate markup for LedgerUserViewModel.
Creates the html so that the name of the html element (which the model binder uses) will map to a property called AccountNumber on the LedgerUser object and not the model itself. Meaning that the model binder is expecting an objec that has a LedgerUser property,
not a ledger user itself.
Now your can have a view model and model work togther with the model binder taking care of the difference, but in those cases you need to hae similar properties on both objects. The model binder works strictly off property names. In those cases the view
model would have properties corresponding to the individual model properites, instead of a single property for the model.
Ricardo D. Sanchez
Palo Software
Website: http://palosoftware.com
Blog: http://ricardodsanchez.com
Twitter: @ricardodsanchez
Please remember to click "Mark as Answer" on this post if it helped you.
Marked as answer by garfbradaz on Apr 19, 2012 07:16 PM
Thanks fot your time. The error was caused because i was returning LedgerUser instead of LedgerUserViewModel. Ive fixed that and it works now. Now i also changed the following
[HttpPost]
public ActionResult Create(bool Thumbnail,LedgerUser ledgerUser, HttpPostedFileBase imageLoad2)
{
}
to
[HttpPost]
public ActionResult Create(bool Thumbnail,LedgerUser LedgerUser, HttpPostedFileBase imageLoad2)
{
This allowed me to reference the LedgerUser model. If i used LedgerUserViewModel there properites in LedgerUserViewModel LedgerUser property were null.
garfbradaz
Member
91 Points
36 Posts
[MVC3] -- The model item passed into the dictionary is of type ERROR
Apr 18, 2012 07:57 PM|LINK
I have a question on stackoverflow here which im stuck on concerning ViewModels and binding the POST:
http://stackoverflow.com/questions/10216691/mvc3-the-model-item-passed-into-the-dictionary-is-of-type-error
If using ViewModel, I was was always under the Impression that the View declared the ViewModel (passed by the GET ActionMethod) but when posting back, the POST ActionMethod declares the Model only and the Model Binder will handle the rest
http://msprogrammer.serviciipeweb.ro/2010/03/29/asp-net-mvc-orm-and-viewmodels/
Am i completely wrong on this?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: [MVC3] -- The model item passed into the dictionary is of type ERROR
Apr 18, 2012 08:23 PM|LINK
I looked at your code and your POST action method needs to receiev an instance of your LedgerUserViewModel and not LedgerUser. The reason for this is because in your view the action helpers are configured to generate markup for LedgerUserViewModel.
This line:
Creates the html so that the name of the html element (which the model binder uses) will map to a property called AccountNumber on the LedgerUser object and not the model itself. Meaning that the model binder is expecting an objec that has a LedgerUser property, not a ledger user itself.
Now your can have a view model and model work togther with the model binder taking care of the difference, but in those cases you need to hae similar properties on both objects. The model binder works strictly off property names. In those cases the view model would have properties corresponding to the individual model properites, instead of a single property for the model.
Blog | Twitter : @Hattan
gdl
Member
533 Points
119 Posts
Re: [MVC3] -- The model item passed into the dictionary is of type ERROR
Apr 19, 2012 01:09 AM|LINK
See my answer in your stackoverflow question:
http://stackoverflow.com/a/10220291/182801
Palo Software
Website: http://palosoftware.com
Blog: http://ricardodsanchez.com
Twitter: @ricardodsanchez
Please remember to click "Mark as Answer" on this post if it helped you.
garfbradaz
Member
91 Points
36 Posts
Re: [MVC3] -- The model item passed into the dictionary is of type ERROR
Apr 19, 2012 08:02 PM|LINK
Hi All,
Thanks fot your time. The error was caused because i was returning LedgerUser instead of LedgerUserViewModel. Ive fixed that and it works now. Now i also changed the following
[HttpPost] public ActionResult Create(bool Thumbnail,LedgerUser ledgerUser, HttpPostedFileBase imageLoad2) { }to
[HttpPost] public ActionResult Create(bool Thumbnail,LedgerUser LedgerUser, HttpPostedFileBase imageLoad2) {This allowed me to reference the LedgerUser model. If i used LedgerUserViewModel there properites in LedgerUserViewModel LedgerUser property were null.
My full working code can be found here:
http://pastebin.com/vcJeNu0A