ASP.NET MVC 3:使用 EF 级联 ComboBox
大家好,StackOverflow 会员们,
我厌倦了把头撞到墙上,所以我想在这里解释一下我的问题,希望能得到解决方案。
我正在使用 C# 和 Razor 开发 ASP.NET 应用程序。我还实现了用于网格显示的 Telerik.MVC.UI。
我现在想做的是,在我的创建表单上显示某种级联组合框。 我在互联网上找到了很多教程,解释了我的数据库结构如何不是 完全相同的。 大多数教程都使用 2 或 3 个表(实体),但我的所有数据字段都在一张表中。
我的表的构造如下: ID Division Department Country
因此,我希望第一个下拉列表显示 SELECT DISTINCT of Country,接下来我希望看到匹配的部门,最后我正在搜索匹配的部门。 如上所述,这一切都在创建表单中,因此在 POST 时,我想将相关 ID 传递给控制器操作。
感谢任何帮助,因为我完全陷入困境......
提前致谢。
Hi fellow StackOverflow Members,
I'm tired of bumping my head on the wall, so I would like to explain my problem here, hoping to get a solution for it.
I'm developing an ASP.NET applications, using C# and Razor. I've also implemented the Telerik.MVC.UI for Grid displays.
What I'm trying to do now is, on my Create form, show some sort of Cascading Comboboxes.
I found a lot of tutorials on the internet, explaining how, however, my database structure isn't
identical.
Most tutorials work with 2 or 3 tables (Entities), but all my data fields are in one table.
My table is constructed as follows:
ID Division Department Country
So I would like to have the first dropdown to show a SELECT DISTINCT of Country, next I would like to see the matching Departments, and last I'm searching for the matching Divisions.
As mentionned above, this is all within a create form, so upon POST, I want to pass the related ID to the Controller Action.
Any help is appreciated, as I'm totally stuck...
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用强类型视图模型。为您的部门、部门和国家/地区创建一个视图模型(基本上是一个包含数据库表中每个字段的列表的类)。在控制器中填充包含数据库中所有三个视图模型,然后将其传递到
返回视图(viewmodel)
。在您看来,您可以使用 someHtmlHelperFor(model.division 或 model.department 等)。Use strongly typed view models. Make a viewmodel for your division, department, and country (basically a class which holds a list<> of each field from your db table). In your controller fill the viewmodel holding all three from the database and then pass it in
return view(viewmodel)
. In your view you can then use someHtmlHelperFor(model.division or model.department,etc.).