使用 Linq2Sql 在 C# asp.net MVC 中创建和更新多对多关系
我按照 NerdDinner 教程在 MVC 中创建了一个 Web 应用程序。我有两个与我的“晚餐”有多对多关系的字段。对于每顿“晚餐”,我需要能够从公司表中选择一个或多个公司,并从服务表中选择一个或多个服务。我已经阅读博客和论坛两天了,但似乎无法弄清楚。我有一个 ServicetoDinner 表,其中包含服务和晚餐的外键,公司也是如此。我知道如何循环显示它们,但我不知道在创建新的“晚餐”时如何选择多个公司和/或服务以及如何保存它们。
我越来越沮丧了。谢谢!
I have created a web app in MVC following the NerdDinner tutorial. I have 2 fields that have many to many relationship with my "dinner". For each "dinner", I need to be able to select one or more Companies from a Company table and one or more Services from a Service table. I've been reading blogs and forums for 2 days, but can't seem to figure it out. I have a ServicetoDinner table with foreign keys to the service and the dinner, and the same for the Companies. I know how to loop through to display them, but I don't know how to select multiple companies and/or services when creating a new "dinner" and how to save them.
I'm getting frustrated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用列表框,您可以通过按住 ctrl 键来选择多个条目。 (确保在显示的页面上对此进行解释)
如果进行编辑,请使用 MultiSelectList 选择已添加的条目(请参阅 让 MultiSelectList 选择项目)
然后在视图上显示控件,使用 Html.ListBox
有点复杂但有用的示例:http://ittecture.wordpress.com/2009/04/30/tip-of -the-day-198-asp-net-mvc-listbox-controls/
Use a ListBox that lets you select multiple entries by holding ctrl. (Make sure you explain this on the page being displayed)
If editing, select the already added entries using MultiSelectList (see Getting MultiSelectList to select items)
Then to show the control on the view use Html.ListBox
A bit over complex but useful example: http://ittecture.wordpress.com/2009/04/30/tip-of-the-day-198-asp-net-mvc-listbox-controls/