Post asp.net mvc for different views in the same way

I have a form area in my opinion. If I press a button A, I want to send to /Books/1, and if I press a button B, I want to send to/Books/2

How to achieve this using MVC?

+3
source share
4 answers

It looks like you want to invoke the book controller using, say, a search action. For example, you can call / Books / Search / <search expression> / 1 or / Books / Search / <search expression> / 2, etc. (There are several different ways to format these URLs, but this is mostly a matter of personal preference.) If you want the URLs to appear as you received them (without acting in the URL), this can be done using routing , something like that:

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

, , WebForms PostBack , , , URL- . , , - - Search - , ViewData, View .

+1
<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

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>
+1

, , , . , , , .

0
source

Source: https://habr.com/ru/post/1698839/


All Articles