如何在 ASP.NET MVC 中以上下文敏感的方式重用逻辑
尝试找出组织 ASP.NET MVC 站点的最佳方式。采用非常简单的 1..N 关系:公司可以有多个联系人,联系人必须恰好有一个公司。
我有你的典型路线:
- Company/Index(列出所有公司)
- Company/Details/{int}(公司 {int} 的详细信息)
- Company/Create(创建新公司)
- Contact/Index(列出所有联系人)
- Contact/Create(创建新联系人,从下拉列表中选择公司)
现在,如果我想创建一个在公司上下文中创建联系人的页面(从公司详细信息页面),以便填写所需的公司/不可编辑),那么会怎样是实现这一目标的最佳途径,同时尽可能不重复代码。
不确定我是否可以利用公司控制器中的联系人/创建逻辑/视图(并能够在完成后路由回公司详细信息页面),或者弄乱路由来执行诸如 Company/Details/{int}/ 之类的操作联系/创建(甚至不确定这是否有意义或是否有效)?
必须有一种更好的方法,然后我添加逻辑和视图,将联系人添加到我的控制器视图中并复制它。
Trying to figure out the best way to organize a ASP.NET MVC site. Take a very simple 1..N relationship: Company can have many Contacts, Contacts must have exactly one Company.
I have your typical routes:
- Company/Index (list all companies)
- Company/Details/{int} (details of Company {int})
- Company/Create (create new company)
- Contact/Index (list all contacts)
- Contact/Create (create new contact, company is selected from drop down)
Now if I wanted to create a page that created a Contact in the context of a Company (from the Company detail page) so that the required company is filled in/not editable), what would be the best route of going about that, while not duplicating code where possible.
Not sure if I can leverage the Contact/Create logic/view from the Company controller (and be able to route back to the Company Details page when complete), or mess with the routes to do something like Company/Details/{int}/Contact/Create (not even sure if that makes sense or would work)?
There has got to be a better way then me adding my logic and view for adding a Contact into my Controller view and having it duplicated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找不到链接,但找到了一些有关使用命名路由的信息来帮助:)
Cant find the link, but found some information about using named routes to help out :)