asp.net mvc以相同的形式发布到不同的视图

时间:2021-02-09 03:22:30

I have form area in my view. If I click button A, I want to submit to /Books/1 and if I click button B, I want to submit to /Books/2

我认为我有表格区域。如果我单击按钮A,我想提交到/ Books / 1,如果我单击按钮B,我想提交到/ Books / 2

How do I achieve this with MVC?

我如何使用MVC实现这一目标?

4 个解决方案

#1


1  

It sounds like what you want to do is call the Books Controller, with, say, the Search action. So for instance you might want to call /Books/Search/<search expression>/1, or /Books/Search/<search expression>/2, etc. (There's a few different ways you could be formatting these URLs, but it's mostly a matter of personal preference I think) If you want the URLs to appear as you've got them above (without the action in the URL), that can be accomplished with routing, something like this:

听起来你想要做的就是调用Books Controller,比如搜索动作。例如,你可能想要调用/ Books / Search / <搜索表达式> / 1,或/ Books / Search / <搜索表达式> / 2等等。(有几种不同的方法可以格式化这些URL,但它是我想这主要是个人偏好的问题)如果你想让URL出现在你上面的那些(没有URL中的动作),那可以通过路由完成,如下所示:

routes.MapRoute(
    "Books",
    "Books/{searchExpr}/{pageId}",
    new { controller = "Books", action = "Search", searchExpr = "", pageId = 1 }
);

I think the main problem is that you're trying to use the WebForms PostBack everything paradigm in a situation where you're probably better off sending the information to the server in the URL or query string. The only time you're actually going to be posting form data here is when the user actually types something into the search box and clicks the Search button - at that point, the controller will pass the search expression to the appropriate View by stuffing it in ViewData, and from there, the View can pull it out and repopulate that textbox on the results page.

我认为主要问题是你在尝试使用WebForms PostBack所有范例时,你可能最好在URL或查询字符串中将信息发送到服务器。您实际上要在此处发布表单数据的唯一时间是用户在搜索框中实际键入内容并单击“搜索”按钮 - 此时,控制器将通过填充搜索表达式将搜索表达式传递给相应的View ViewData,从那里,View可以将其拉出并在结果页面上重新填充该文本框。

#2


3  

<form id="form1" name="form1" action="/Books/" method="get">
<input type="text" name="search" value="">
<input type="submit" name="id" value="1">
<input type="submit" name="id" value="2">
</form>

#3


1  

MVC Views can have multiple forms on a 'page', so just create separate sections and give each one their own form action.

MVC视图可以在“页面”上有多个表单,因此只需创建单独的部分并为每个部分提供自己的表单操作。

<form id="form1" name="form1" action="/Books/1" method="get">
<!--...form fields-->
</form>


<form id="form2" name="form2" action="/Books/2" method="get">
<!--...form fields-->
</form>

#4


0  

I have never seen the ability to have a form field attached to two forms, seems like it wouldn't work. What you can do is put a hidden field in the second form which, on submission, grabs the information from the textbox in the first form.

我从未见过将表单字段附加到两个表单的能力,似乎它不起作用。您可以做的是在第二种形式中放置一个隐藏字段,在提交时,从第一种形式的文本框中获取信息。

#1


1  

It sounds like what you want to do is call the Books Controller, with, say, the Search action. So for instance you might want to call /Books/Search/<search expression>/1, or /Books/Search/<search expression>/2, etc. (There's a few different ways you could be formatting these URLs, but it's mostly a matter of personal preference I think) If you want the URLs to appear as you've got them above (without the action in the URL), that can be accomplished with routing, something like this:

听起来你想要做的就是调用Books Controller,比如搜索动作。例如,你可能想要调用/ Books / Search / <搜索表达式> / 1,或/ Books / Search / <搜索表达式> / 2等等。(有几种不同的方法可以格式化这些URL,但它是我想这主要是个人偏好的问题)如果你想让URL出现在你上面的那些(没有URL中的动作),那可以通过路由完成,如下所示:

routes.MapRoute(
    "Books",
    "Books/{searchExpr}/{pageId}",
    new { controller = "Books", action = "Search", searchExpr = "", pageId = 1 }
);

I think the main problem is that you're trying to use the WebForms PostBack everything paradigm in a situation where you're probably better off sending the information to the server in the URL or query string. The only time you're actually going to be posting form data here is when the user actually types something into the search box and clicks the Search button - at that point, the controller will pass the search expression to the appropriate View by stuffing it in ViewData, and from there, the View can pull it out and repopulate that textbox on the results page.

我认为主要问题是你在尝试使用WebForms PostBack所有范例时,你可能最好在URL或查询字符串中将信息发送到服务器。您实际上要在此处发布表单数据的唯一时间是用户在搜索框中实际键入内容并单击“搜索”按钮 - 此时,控制器将通过填充搜索表达式将搜索表达式传递给相应的View ViewData,从那里,View可以将其拉出并在结果页面上重新填充该文本框。

#2


3  

<form id="form1" name="form1" action="/Books/" method="get">
<input type="text" name="search" value="">
<input type="submit" name="id" value="1">
<input type="submit" name="id" value="2">
</form>

#3


1  

MVC Views can have multiple forms on a 'page', so just create separate sections and give each one their own form action.

MVC视图可以在“页面”上有多个表单,因此只需创建单独的部分并为每个部分提供自己的表单操作。

<form id="form1" name="form1" action="/Books/1" method="get">
<!--...form fields-->
</form>


<form id="form2" name="form2" action="/Books/2" method="get">
<!--...form fields-->
</form>

#4


0  

I have never seen the ability to have a form field attached to two forms, seems like it wouldn't work. What you can do is put a hidden field in the second form which, on submission, grabs the information from the textbox in the first form.

我从未见过将表单字段附加到两个表单的能力,似乎它不起作用。您可以做的是在第二种形式中放置一个隐藏字段,在提交时,从第一种形式的文本框中获取信息。