In today's MVC test, I want to pass multiple parameters in the route, such as blog/archtive/2010-04-18/4. in HTML, the parameters are two. One is time: 2010-04-18, and the other is serial number: 4.
After one day of verification and testing, I finally found the solution and verified the two solutions (like a ^_^). Due to the beginner MVC, I encountered a lot of problems in the test, in particular, when testing the first solution, the initial thought was to adopt a URL with multiple parameters. However, I did not know what was going on. I did not pass the verification all morning and had no way to seek other solutions, after I passed the verification in the second solution, I went back to the test in the first solution. I did not know which part was not set during the morning test, I thought the original idea would not work! If you don't want to talk about it, share the results with everyone and hope to help everyone.
Method 1:
1) Add a route table as follows:
Routes. maproute (
"Default", // route name
"{Controller}/Your action=.html/{ID}", // URL with Parameters
New {controller = "home", Action = "Index", id = ""} // parameter defaults
);
Routes. maproute ("blogactive ",
"Blog/archtive/{date}/{ID }",
New {controller = "blog", Action = "archtive "},
New {date = @ "\ D {4}-\ D {2}-\ D {2 }"});
2) The index. aspx file in the view-> Home file has the following link:
<Li> <% = html. actionlink ("My link", "archtive/2010-04-18/4", "blog") %> </LI>
3) Add the function in controllers> blogcontroller. CS:
Public actionresult archtive (string date, string ID)
{
Viewdata ["date"] = date;
Viewdata ["ID"] = ID;
Return view ("archtive ");
}
4) Add the following content to the archtive. aspx file in the view-> blog Folder:
The log time to be queried is: <% = viewdata ["date"] %> <br/>
The log ID to be queried is: <% = viewdata ["ID"] %> <br/>
5) enter the URL: http: // localhost/mvctest/home/index.html. the following link is displayed on the page: http: // localhost/mvctest/blog/archtive/2010-04-18/4. html
6) click the link on the page and go to http: // localhost/mvctest/blog/archtive/2010-04-18/4. HTML, the page will display the following content: the log time to be queried is:
The ID of the log to be queried is 4.html. The preceding two parameters are obtained if the route is correct.
Method 2:1) Add the following in the routing table:
Routes. maproute ("blogactive ",
"Blog/archtive/{* ID }",
New {controller = "blog", Action = "archtive "}
);
2) The index. aspx file in the view-> Home file has the following link:
<Li> <% = html. actionlink ("My link", "archtive/2010-04-18/4", "blog") %> </LI>
3) Add the function in controllers> blogcontroller. CS:
Public actionresult archtive (string ID)
{
Viewdata ["date"] = ID;
Return view ("archtive ");
}
4) Add the following content to the archtive. aspx file in the view-> blog Folder:
The log time to be queried is: <% = viewdata ["date"] %> <br/>
5) enter the URL: http: // localhost/mvctest/home/index.html. the following link is displayed on the page: http: // localhost/mvctest/blog/archtive/2010-04-18/4. html
6) click the link on the page and go to http: // localhost/mvctest/blog/archtive/2010-04-18/4. the following content is displayed on the HTML page: 2010-04-18/4. the preceding instructions in HTML indicate that the route is correct and the input parameters are obtained. However, this method uses 2010-04-18/4. HTML is passed as a parameter. To get the results of the two parameters, You need to parse them further before using them.