是否可以将一个 .net ASP MVC 控制器用于两个“面板”?
我正在学习 asp mvc 模式,并且对控制器有点迷失。我正在构建的网页需要在左侧有一个搜索“面板”,在右侧有一个数据窗口。
这是一个或两个控制器的任务吗?我需要的是用户在搜索面板上输入数据,单击“开始”,然后在该面板中显示结果。
在一个控制器下,这一切似乎都很容易,但是......
我想单击搜索面板中的结果之一并在右侧面板中显示其数据,但这是否是新控制器的工作?看起来像是两个控制器直观地控制每个面板,但这可能吗?
I am learning the learning the asp mvc pattern and am a little lost on controllers. The web page I am building needs to have a search "Panel" on the left, and a data window on the right.
Is this the task for one controller or two? What I need is the user to enter data on the search panel, click go and then display results in that panel.
That all seems pretty easy under one controller but...
I want to click on one of the results in the search panel and display their data in the right panel but is that the job for a new controller? It seems like two controllers intuitively to control each panel but is that even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所描述的内容可以通过多种方式实现,但这听起来有点像您正在考虑像页面这样的“控制器”。控制器包含多个方法,每个方法都会执行一个操作(可能是页面渲染)。控制器并不代表网页,而是执行的操作的集合(重定向、GET、POSTS 等)。
您可能只有一个控制器,但有两种用于搜索的 Get 和 Post 阶段的方法。如何填充每个面板会带来很多问题:
也许最容易开始的是第一个选项 - 在搜索时强制刷新页面。
然后是快速简单的视图:
从这里开始,将其与 Ajax 连接起来相当简单。
编辑 - 第 2 步 - ajax 内容。
控制器操作不必返回整个视图 - 它们可以返回 ajax 请求的一部分。
因此,至少为面板 2 添加部分内容:
Index.cshtml:
创建一个新视图,Panel2.cshtml
然后更改控制器操作:
唯一剩下的就是一些将它们链接在一起的 javascript(不能完全完成这一切)我的头顶)
What you're describing could be achieved numerous ways, but it sounds a bit like you're thinking of a 'controller' like a page. A controller contains multiple methods, each of which will execute an action (which possibly could be a page rendering). A controller does not represent a web page, but a collection of actions performed (redirects, GET's, POSTS, etc...).
Chances are you'll only have one controller, but two methods for the Get and Post phase of searching. How you populate each panel brings up a lot of questions:
Perhaps the easiest to start with is the first option - forcing a page refresh on search.
Then a quick and easy view:
From here, hooking it up with Ajax is fairly straight forward.
Edit - Step 2 - ajax stuff.
Controller actions don't have to return a whole view - they can return a partial to an ajax request.
So, add a partial at least for panel 2:
Index.cshtml:
Create a new view, Panel2.cshtml
Then change the controller action:
The only left is some javascript to link it all together (can't quite do this all off the top of my head)
您的搜索面板和输出(结果)面板似乎都位于搜索“上下文”中。让同一个控制器处理这些是完全可以接受的。
当人们开始 MVC 时,他们得到了很多建议,为每个实体配备一个控制器。这是在 CRUD 上下文中学习 MVC 的好建议,但在移动传递的实体 CRUD 时可能会导致一些混乱。
根据您的情况选择 SearchController 。
Both your search panel and your output (results) panel seem to be in a search 'context'. It's perfectly acceptable to have these handled by the same controller.
When people are beginning MVC they got a lot of advice to have a controller for each Entity. This is good advice to learn MVC in a CRUD context but can lead to a little bit of confusion when moving passed Entity CRUD.
Go with SearchController in your case.