ASP.NET MVC – No parameterless constructor defined for this object

So, I have been bitten by this error roughly three times now and every time I forget what it was that I did to fix it. So this time, I thought I would blog about it and maybe it would stick this time and at the same time maybe help someone else out with this error.

I think the reason the fix for this error is so easily forgotten is because the error itself has absolutely nothing to do with the above message. Every time I have seem this error, ultimately the problem was related to the misuse of the SelectList HTML helper. Here is how the select list is supposed to work.

@Html.SelectList(model => model.SelectedItem, Model.SelectList);

So what this is doing is using the model’s SelectedItem property (custom property you create) as the slot to place the item the user selects from the list. The second paramater is the list of items that the user should choose from in the form of a SelectList. This is a big key right here. You must use a SelecList type for this list or something that you can cast as a selectList in the view itself (not recommended). The reason I don’t recommend this is because things like this can fail and it is much easier to recover from a bad cast in code that it is once the View is being put together.

Take care to make sure that the SelectList is in the proper format and is indeed of type SelectList and not some other type that you think is of type SelectList. if you need any help with converting a SelectList generically to something else, feel free to ask, or see my post on converting Enums to Selectlists here to get you started.

I hope this helps other people from following a similar rabbit hole.

 

Happy Coding Smile

Advertisements

About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, ASP.NET, C#, Classes, Exception handling, Exceptions, MVC, Performance, Programming, Validation and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s