Talking about how to pass multiple parameters in Asp.net Mvc Action, asp. netmvc
Recently, you need to query the log file information, view a specific log information, and view other log information lists of the date of the log.
To complete this function, I plan to input two parameters in the URL, one to record the log time and the other to record the log's primary key ID. Therefore, I want to start with the Asp.net MVC route.
In the Global. asax file, the default route is as follows.
Routes. mapRoute ("Default", // route name "{controller}/{action}/{id}", // URL with parameters new {controller = "Logon ", action = "Logon", id = UrlParameter. optional} // default value );
In this route, only one parameter can be entered after the Action, and multiple parameters cannot be entered. Therefore, you need to add route information.
Add a route entry named "Default1" in the Global file. The Code is as follows:
// The first route transmits two parameters to the routes. mapRoute ("Default1", "{controller}/{action}/{Parma1}/{Parma2}", new {controller = "", action = ""}, new {});
You can input two parameters for the preceding routing.
Here, we create a TestController, add a Test. cshtml page, and write the code in TestController, as shown below:
public ActionResult Test(string date, string id){ViewData["date"] = date;ViewData["id"] = id;return View();}
Write the following code on the Test. cshtml page:
The log time to be queried is @ ViewData ["date"] <br/> the log ID to be queried is @ ViewData ["id"] <br/>
Run the compiler and enter "http: // localhost: 11507/Test/2013-12-18/5" in the browser. The page is displayed as follows:
The log time to be queried is
Log ID: 5
Now there is another problem. We need to input multiple parameters. What should we do? Of course, only the new route "Default2" is added ". The Code is as follows:
// The second route transmits multiple parameters, only the data routes after the second underline is obtained. mapRoute ("Default2", "{controller}/{action}/{* id}", new {controller = "", action = ""});
Run the compiler and enter "http: // localhost: 11507/Test/2013-12-18/5/xianrongbin" in the browser. The page is displayed as follows:
The log time to be queried is:
Log ID: 2013-12-18/5/xianrongbin
Here we can only get all the parameters following the Action. For example, the log time is "", the log ID is "5", and the LOG Operator is "xianrongbin ".
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.