从表单调用另一个视图的 ActionResult
我正在尝试在视图 1 的 MVC 应用程序中创建一个搜索表单。当我将表单提交到同一页面 (View1) 时,该表单工作得非常好。这样我就可以有两个 ActionResults - 其中一个接受 HttpPost 请求。这里一切都很酷
现在事情变得有点复杂,我希望分开观点。所以在 View1 中有一个表单,我希望结果显示在 View2 中。那么如何从 View1 中的表单调用 View2 的 ActionResult 呢?
简而言之 - 用户在 View1 中输入关键字。按回车键。视图 1 中的窗体调用视图 2。 View2 中的 ActionResults 调用一些逻辑来搜索并返回 View2 作为视图,然后我可以显示结果。
我尝试了一些基本的东西,比如 action="/View2" 但我很确定它会失败。它说“找不到资源”。有可能做到这一点吗?请提供建议。
*更新*
可以按照下面的回答解决。
使用 Html.BeginForm(ActionResult 的名称,控制器的名称)
I am trying to create a search form in my MVC application in View 1. The form was working very well when I used to submit the form to same page (View1). That way I can have two ActionResults - one of which accepts HttpPost requests. Everything is cool here
Now things have become slighly complex and I wish to separate the views. So in View1 there is a form and I wanted the results to be displayed in View2. So how do I call ActionResult of View2 from a form in View1?
In short - User enters keyword in View1. Hits Enter. Form in View 1 calls View2. ActionResults in View2 calls some logic to search and return View2 as the view and then I can display the results.
I tried some basic things like action="/View2" but I was pretty sure it would fail. It says 'the resource cannot be found'. Is it even possible to do this? Kindly advice.
*UPDATE*
It can be solved as answered below.
use Html.BeginForm(Name of the ActionResult,Name of the controller)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在表单中指定其他操作的 URL,最好通过调用
Html.Form("ActionName", "ControllerName")
帮助器。You need to specify the URL of the other action in the form, preferably by calling the
Html.Form("ActionName", "ControllerName")
helper.