I’m using ASP.NET MVC 3 and I post a form in my view, containing a @Html.ListBoxFor
When I receive the posted form as a FormCollection, how can I check if an item was selected in the ListBox?
In my controller there seems to be no item named collection["Company.RepresentingCountries"] since no <select> option was selected.. This results in a “Object reference not set to an instance of an object.” error message when I try to check for it! What’s the protocol here?
Thanks!
You haven’t shown how your
ListBoxForhelper is defined, so I can only be guessing here. This being said you talked aboutFormCollectionwhich usage I wouldn’t recommend. What I recommend is using view models. So let’s take an example:Model:
Controller:
View: