使用复选框选择项目

发布于 2024-11-04 18:47:40 字数 257 浏览 0 评论 0原文

我正在构建一个 silverlight 应用程序。我的要求是让用户从更大的技能列表中选择一些适用于他们的技能。

表:候选人 =>候选人技能;技能类别 =>技能。我认为该模式是不言自明的。前端将显示所有技能(分为不同类别),当候选人登录时,只有他选择的技能才会显示在复选框中。相当简单。

我的问题:我是否将所有技能实体带到前端,然后获取 CandidateSkill 实体,循环遍历它们并相应地设置复选框,还是它们是更简单/更好的方法?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋意浓 2024-11-11 18:47:40

我建议构建一个类来用作 ViewModel。该类应至少包含一个属性来指示是否选择了该项目、要呈现的文本以及模型实体本身或其键。

您可以通过将所有技能集左连接到单个候选人的技能,并将 IsSelected 设置为对候选人技能进行非空测试的结果来创建视图模型对象集。

然后您可以直接绑定到 ViewModel。

我曾经遇到过类似的情况(用户权限而不是候选人技能),我使用了 此资源作为起点。我希望它有帮助。

就我而言,我有一个“保存”按钮,单击该按钮后,将运行一些代码隐藏代码来迭代选定的项目并将它们提交到我的 Web 服务。在不了解您的数据和服务实现的详细信息的情况下,我不会用具体的细节来混淆帖子。

祝你好运!

讨论评论

这是一个通过发出两个数据库调用来创建视图模型的伪 LINQ 过程:

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;

这将为您提供一个可绑定的数据源。最终,使用此模式,您必须手动将选择/取消选择转换为插入/删除。对我来说,我在按钮单击的处理程序中执行此操作。我检索一组新的候选技能,迭代列表中的项目,并根据需要插入/删除 CandidateSkill 的实例。

我意识到,纯粹主义者可能不会认为依靠单击按钮将视图模型状态解析为数据库操作是完整的 MVVM,但它对我有用。

我希望这能有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文