Greg
Greg

Reputation: 2690

Using check boxes to select Items

I am building a silverlight app. My requirement is to have Users select a number of skills that apply to them, from a larger list of skills.

Tables: Candidate => CandidateSkills; SkillsCategories => Skills. I think the schema is self explanatory. The front end will show all skills (grouped into different categories), and when the candidate logs in, only his selected skills will show in the check boxes. Fairly simple.

My question: Do I bring all the Skill entities to the front end and then get the CandidateSkill entities, loop through them and set the checkboxes accordingly or is their a easier/better way?

Thanks

Upvotes: 1

Views: 82

Answers (1)

kbrimington
kbrimington

Reputation: 25642

I recommend building a class to use as a ViewModel. The class should contain at least a property to indicate whether the item is selected, the text to present, and either the model entity itself or its key.

You can create the set of view model objects by left-joining the set of all skills to the individual candidate's skills, and setting IsSelected to the result of a non-null test on the candidate skill.

You can then bind directly to the ViewModel.

I had a similar situation (Users to Permissions instead of Candidates to Skills) once, and I used this resource as a starting point. I hope it helps.

In my case, I had a "Save" button which, upon click, would run some code-behind code to iterate through the selected items and submit them to my Web service. Without knowing the details of your data and service implementation, I'll not clutter up the post with the nitty-gritty details.

Best of luck!

Comments for Discussion

Here is a pseudo-LINQ procedure creating view models by issuing two database calls:

var userskills = database.CandidateSkills
                       .Where(cs => cs.UserId == someUserId)
                       .Select(cs => cs.SkillId)
                       .ToList();
var skills = from s in database.Skills
             select new CandidateSkillViewModel()
             {
                 Text = s.SkillName,
                 IsSelected = userskills.Contains(s.SkillId),
                 Value = s.SkillId
             };
mylist.ItemsSource = skills;

This would give you a bindable data source. Ultimately, using this pattern, you'll have to translate selections/deselections into inserts/deletes by hand. For me, I do this in the handler for the button click. I retrieve a fresh set of candidate skills, iterate through the items of the list, and insert/delete instances of CandidateSkill as needed.

I realize that depending on a button click to resolve my viewmodel state into database operations might not be considered by purists to be complete MVVM, but it worked for me.

I hope this helps a little more.

Upvotes: 1

Related Questions