MVC: Action Methods with Parameters

Please see my other MVC articles.

Passing arguments to action methods.

It’s often necessary when calling an action method to pass to it variables necessary for business logic.
In this article, I will illustrate one method that is similar to the Web Forms programming model of using the question mark + equals sign convention (?VariableName=Value).

To begin, I first created a simple HelloWorld controller in which I placed an action method “Welcome().”
Notice that this method receives two parameters, “name” and “numTimes.”

1-action-methods-with-parameters

Now I’ll run my application while passing the variables “?name=earnie&numtimes=4.”
As you can see, the Welcome() method parsed my name and the number of times I wished to display a welcome message to myself.

2-action-methods-with-parameters

Also, I may elect to use an action method that returns a simple string result instead of a View.

3-action-methods-with-parameters

When I execute the action method, you can see the results are similar.

4-action-methods-with-parameters

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 )

Connecting to %s