MVC2 - 如何在每个页面上放置常见的逻辑控件(如搜索/查找)?
我无法弄清楚如何执行以下操作:
在每个页面(或我想要的每个页面)上,我想放置一个通用控制小部件(例如,认为 - 包含文本框+按钮的搜索功能)。执行此操作的最佳方法是什么?谁处理提交按钮(假设它是提交按钮)?
即我的 ViewUserControl 是什么样子?它有表格吗?它使用 jQuery onclick"" 吗?它是否发布到主视图的操作方法,或者我可以将其重定向到另一个控制器/操作吗?
我尝试使用包含表单的“Search.ascx”的 RenderAction,并由我的 SearchController 处理...但在 SearchController 中,它然后尝试调用 RedirectToAction...并且我收到关于不允许 RedirectActions 的投诉儿童行动。
我有点不知道下一步该做什么,所以非常欢迎提出建议! 射线
I'm having trouble figuring out how to do the following:
On every page (or every page I so desire), I'd like to put a common control widget (e.g. think - Search functionality that contains a textbox+button). What's the best way to do this, and who handles the submit button (assuming it is a submit button)?
i.e. what does my ViewUserControl look like? Does it have a form? does it use jQuery onclick""? Does it post to the main View's action method, or can I redirect it to another Controller/Action?
I have tried using RenderAction of a "Search.ascx" which contains a Form, and is handled by my SearchController... but in the SearchController, it then tries to call RedirectToAction... and I get a complaint about RedirectActions not allowed on Child Actions.
I'm a bit lost on what to do next, so suggestions greatly welcome!
Ray
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您似乎走在正确的轨道上(使用
ViewUserControl
和RenderPartial
)。但是根据您提供的信息,很难看出您的其他问题是什么(RenderAction,...)这很容易:
You seem to be on the right track (using
ViewUserControl
andRenderPartial
). But with the information you have provided, it is not easy to see what you other problems are (RenderAction , ...)It is easy:
显示 HTML 元素的最佳方法是将它们放入母版页或母版页引用的部分中。我希望它是它自己的表单并提交给您的 SearchController。
如果您想了解更多详情,请告诉我。
The best way to have the HTML elements show up is to put them in a master page, or in a partial that is referenced by your master page. I would have it be it's own form and submit to your SearchController.
Let me know if you want more particulars.
RenderPartial 就是这里的方法。您的 control.ascx 将包含其表单和提交按钮。
RenderPartial is the way to go here. Your control.ascx will consist of it's form and submit button.
可能是片面的观点。 .ascx 文件。
局部视图
是的,它有一个形式。它应该尽可能独立。
好吧,无论什么最适合您的具体场景。它可能应该发布到最合适的操作。这不太可能是主视图的操作,因为部分视图在不同的父视图中重用。
您可能希望在父视图中使用 RenderPartial 来渲染它:
Probably a partial view. An .ascx file.
The partial view
Yes, it has a form. It should be as self contained as possible.
Well, whatever fits your exact scenario best. It should probably post to whichever action is most appropriate. That is not likely to be the main view's action, since the partial is reused in different parent views.
You'll probably want to render it using RenderPartial in the parent view:
好吧,我知道我的问题是什么了。上面的回复都是正确的。我有自己的 Search.ascx 用户控件和 SearchController,我还使用了 RenderPartial,但令我困惑的是我忘记显式指定控制器/操作...所以然后我在我的 onclick 事件和 Url.Action 上摆弄按钮代替。
感谢所有回复的人。
Ok, I figured out what my problem was. The replies above are correct. I have my own Search.ascx user control and SearchController, I also used RenderPartial, but what stumped me was that I forgot to explicitly specify the controller/action... so then I was fiddling around with onclick events and Url.Action on my button instead.
Thanks to all who replied.