First, create a new Web project and create a new home controller with the following default code:
Public class Homecontroller:controller { // get:home Public actionresult Index () { return View (); } }
Now I'm going to the index method, pass a parameter, ID, for the convenience of testing, appropriate modification of the code
Public class Homecontroller:controller { //get:home publicIndex(int?) ID) { "id=" + ID; } }
Then run the program and the result is ...
Enter in the browser: home/index/200, you get the results you want. That is, to the action method, a parameter ID is passed with a value of 200.
Above is to pass a parameter, however, in the actual work, we will not only to the Controller's action method, pass a parameter, then pass multiple parameters How to do it???
Public classHomecontroller:controller {//Get:home Public stringIndex (int? IdstringNamestringsex) { return "id="+ id+"<br/>"+"name="+name+"<br/>"+"sex="+sex; } }
This is where multiple parameters are passed.
Run the code and the result is:
In the browser, type: Home/index/10?name= Sun Quan? sex= Shemale
Note: The parameters entered here in the browser: Name,sex to be the same as the parameter name of the action method in the controller, it doesn't matter if the case is uppercase or lowercase.
Obviously this is not the result we want, how to do it, for a moment to think of a good way, I modified the route:
Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}/{name}/{sex}", defaults:New{controller ="Home", action ="Index", id = urlparameter.optional,name=urlparameter.optional,sex=urlparameter.optional}); }
And then, it came to pass:
To pass multiple parameters, do this:
Record it
MVC Learning Series 2--passing parameters to the action method