MVC2 - 如何在每个页面上放置常见的逻辑控件(如搜索/查找)?

发布于 2024-10-12 02:09:16 字数 414 浏览 3 评论 0原文

我无法弄清楚如何执行以下操作:

在每个页面(或我想要的每个页面)上,我想放置一个通用控制小部件(例如,认为 - 包含文本框+按钮的搜索功能)。执行此操作的最佳方法是什么?谁处理提交按钮(假设它是提交按钮)?

即我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

℡寂寞咖啡 2024-10-19 02:09:17

您似乎走在正确的轨道上(使用 ViewUserControlRenderPartial)。但是根据您提供的信息,很难看出您的其他问题是什么(RenderAction,...)

这很容易:

  • 创建一个 UserControl (.ascx) 并在其中获取一个 URL 为 /search/ 的表单。 ......,你可以拿回来的东西。
  • 在您的视图中,调用 RenderPartial 并提供视图名称
  • 创建控制器以接收来自搜索的帖子。这与您的父视图控制器不是同一个控制器。

You seem to be on the right track (using ViewUserControl and RenderPartial). But with the information you have provided, it is not easy to see what you other problems are (RenderAction , ...)

It is easy:

  • Create a UserControl (.ascx) and get a form in there with URL being /search/..., something that you can get back.
  • In your views, call RenderPartial and provided the view name
  • Create your controller to receive the post from your search. This is not the same controller as your parent view controller.
影子的影子 2024-10-19 02:09:17

显示 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.

宫墨修音 2024-10-19 02:09:17

RenderPartial 就是这里的方法。您的 control.ascx 将包含其表单和提交按钮。

RenderPartial is the way to go here. Your control.ascx will consist of it's form and submit button.

陌伤浅笑 2024-10-19 02:09:17

最好的方法是什么

可能是片面的观点。 .ascx 文件。

以及谁处理提交按钮
(假设它是一个提交按钮)?

局部视图

我的 ViewUserControl 是什么样子的
喜欢?有表格吗?

是的,它有一个形式。它应该尽可能独立。

它使用 jQuery onclick"" 吗?是吗
发布到主视图的操作方法,
或者我可以将其重定向到另一个
控制器/操作?

好吧,无论什么最适合您的具体场景。它可能应该发布到最合适的操作。这不太可能是主视图的操作,因为部分视图在不同的父视图中重用。

我尝试过使用 RenderAction
“Search.ascx”包含一个表单,
由我处理
SearchController...但是在
SearchController,然后它尝试
调用 RedirectToAction...我得到一个
关于 RedirectActions 的投诉不
允许子操作。

您可能希望在父视图中使用 RenderPartial 来渲染它:

<%: Html.RenderPartial("MyPartialView.ascx") %>

What's the best way to do this

Probably a partial view. An .ascx file.

and who handles the submit button
(assuming it is a submit button)?

The partial view

what does my ViewUserControl look
like? Does it have a form?

Yes, it has a form. It should be as self contained as possible.

does it use jQuery onclick""? Does it
post to the main View's action method,
or can I redirect it to another
Controller/Action?

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.

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.

You'll probably want to render it using RenderPartial in the parent view:

<%: Html.RenderPartial("MyPartialView.ascx") %>
葬シ愛 2024-10-19 02:09:17

好吧,我知道我的问题是什么了。上面的回复都是正确的。我有自己的 Search.ascx 用户控件和 SearchController,我还使用了 RenderPartial,但令我困惑的是我忘记显式指定控制器/操作...所以然后我在我的 onclick 事件和 Url.Action 上摆弄按钮代替。

<% using (Html.BeginForm("MySearchAction", "MySearchController")) { %>

感谢所有回复的人。

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.

<% using (Html.BeginForm("MySearchAction", "MySearchController")) { %>

Thanks to all who replied.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文