I am trying to use the checkboxlist control from mvccbl.com as below:
For examples see: http://mvccbl.com/Examples
@Html.CheckBoxListFor(x => x.PostedCities.CityIDs, // checkbox list name, 'PostedCities.CityIDs' in this case
x => x.AvailableCities, // List<City>()
x => x.Id, // City.Id
x => x.Name, // City.Name
x => x.SelectedCities) // List<City>() - should contain only cities to be selected
My requirement is that SelectedCities should be lazy loaded. So I am loading when the user clicks the Add/Edit button. Since there are several checkbox list controls on the editor page and all of them are required, so I am using data annotations [Required] on the posted values. The problem is that if user forgets to check a box in one list and tries to update, model validation fails and that causes the popup editor to reload the data and thereby user selections are lost.
Any ideas on how to fix this?
6 Answers, 1 is accepted
How are you loading the data on demand? Could you provide the code you are using? The checked state will not be preserved if the items are not populated from the ModelState on the server.Regards,
The code in the forum thread that you provided does not show how the data is loaded and is used in your scenario. Could you provide the code that you are actually using? Also, when using server editing you could also check the parameter on the server and load the data when needed:Regards,
I will try to create a simple project and upload it.
The selection is always determined based on the ID instead of the posted values, so the state will be lost. You should use the posted values instead when the form is posted to the update action. I attached the project modified to show how can this be achieved for the CustomerTypes.Regards,