Inside your HttpPost action you cannot expect the AvailalableEmployee and SelectedEmployee collections to be populated. That's due to the nature of how HTML works. Only the selected values are sent to the server when you submit the form. The text values of the select fields are never sent to your server when you submit the form.
So all you can hope of getting populated inside this HttpPost action is the AvailableEmpCodeArray and the SelectedEmpCodeArray collections. They will contain the values of the selected items in the corresponding select fields. Notice that I have bolded selected. This means that only if you select any items in the corresponding boxes those collections will be bound. If you don't select anything, they will remain null and that's perfectly normal.
Here's an example of the user that has selected nothing. He just used the << and >> buttons to toggle the values between the lists, but please notice how absolutely nothing is selected:
Notice how nothing is selected? So that's all you will get in your controller action - nothing.
Now please compare with the following screenshot:
Do you see the difference? Notice how the user explicitly selected the Emp2, Emp5 and Emp6 values (using the Shift key to select multiple values)? That's what you're gonna get inside your HttpPost action. The AvailableEmpCodeArray collection will contain a single element with the value of 2 and the SelectedEmpCodeArray will contain 2 elements with the values of 5 and 6.
Now to your second problem. If you intend to redisplay the same view from your HttpPost controller action the first thing you need to ensure is that you have assigned values to the AvailalableEmployee and SelectedEmployee collections. This should be done the same way you did in the HttpGet action:
or if those values are coming from a database you will have to re-query this database inside the HttpPost action because those values, as I already explained, will never get bound from the view.
Thanks Darine for the quick response. How can I get the values stored in both the listboxes? User will select the values from one listbox to another and then perform other operations before clicking on the submit button.
The user must select the values if you want those values to be sent to your server. That's how HTML works and please learn the basics of HTML. If the user doesn't select anything no matter that you have toggled them between the select boxes, nothing's gonna be sent to the server. I will update my answer with screenshots for you to understand what I mean.
Yes I am new to HTML and learning basics of HTML...Is there any other way I can get values in controller?
I have updated my answer with screenshots. Hopefully you will now understand. You must select the values that you want to be sent to the server. That's how HTML works. Also if you are new to HTML I very strongly suggest you heading towards the w3.org site and start learning it before even getting into ASP.NET MVC. If you don't know the basics of HTML, you really don't need to waste your time with ASP.NET MVC because you will find lots of things strange and you will have hard time understanding them.
Thanks Darin....In my case values are selected when they are moved but then user can perform other operation such as modify department name, unselect some other values. When user clicks on submit then the changes should get saved to the database. In order to do that I need to know original list of values in both the listboxes and the changed list....So looks like I will have to find someother way to do this.