Home > ASP.NET MVC > ASP.NET # MVC # 4 – ASP.NET MVC Controller(Action methods with multiple parameters)

ASP.NET # MVC # 4 – ASP.NET MVC Controller(Action methods with multiple parameters)

Hi friends,

  In one of my previous blog

 [https://microsoftmentalist.wordpress.com/2011/06/27/asp-net-mvc-controllers-in-detail/ ]

[Please copy above  link and paste in a url if you get any proble  while opening the page.]

we saw the action methods with a single parameter,In this blog we will see the action methods with multiple parameters.

Action Methods with multiple parameters

*Suppose you want to add three numbers and you have to pass the parameters from the Url .

public void Add(int x, int y, int z)     

{             Response.Write( “Addition of x,y,z =”+(x + y + z).ToString());        

}

So in above method We have to pass the parameters 1,2,3 to the method.

*To call this method we will define a route that allows you to specify the parameters
in a cleaner format. This code goes inside the RegisterRoutes methods within the Global.asax.cs
file, and uses the MapRoute method (discussed in later blog) to define a new route:

routes.MapRoute(
                “Test”, // Route name
                “{controller}/{action}/{x}/{y}/{z}”, // URL with parameters
                new { controller = “Home”, action = “Add”, id = UrlParameter.Optional } // Parameter defaults
            );

*You can call Add method by just writing following URL:-

/Home/Add/1/2/3

You will have o/p as follows

image

Advertisements
Categories: ASP.NET MVC
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: