Pass parameter or Query String in Action Method in ASP.NET MVC


In this Article, We will learn How to Pass parameter or Query String in an Action Method in ASP.NET MVC. This is second part of the ASP.NET MVC 4.0 tutorial series. In the first part, We saw How to create First Application in ASP.NET MVC 4.0

Before proceeding, I recommend you to read the previous article of this series (Create First Application in ASP.NET MVC 4.0). In this Article, We will learn How to Pass parameter or Query String in an Action Method in ASP.NET MVC.

Let's Begin:
1) Create a New ASP.NET MVC4 Empty Project.
2) Add a controller -> Named it as HomeController -> Add Index Action Method to it. If you don't know How to create or How to add controller Read it
Here.
3) Pass parameter in Index Action Method.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PassParameter.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/

public string Index(string id)
{
return "ID =" + id;
}

}
}

In the above code, we have passed a parameter named as the id of string data type. Now Build & Run the application and open http://localhost:13923/Home/Index/10 link in the URL by passing the parameter to Index Action Method of Home Controller.
Preview:
Pass parameter in ASP.NET MVC


Passing Multiple Parameter in Index Action Method:
In the above example, we saw How to pass single parameter in Action Method But there are many situations when we have to pass more than one parameter. Let's see How to do this in ASP.NET MVC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PassParameter.Controllers
{
public class HomeController : Controller
{

public string Index(string id, string name)
{
return "ID =" + id+"<br /> Name="+name;
}

}
}


In the above code, we have added/passed multiple parameter (two) in Index Action method. Name of parameter in the URL must match with the Name of the
parameter in the Action Method.
Preview:
Pass parameter in ASP.NET MVC

We can also get the QueryString values using Request.QueryString Collection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PassParameter.Controllers
{
public class HomeController : Controller
{

public string Index(string id)
{
return "ID =" + id+"<br /> Name="+Request.QueryString["name"].ToString();
}

}
}


Preview:

Pass parameter in ASP.NET MVC
I hope you enjoyed this post. If you have any query comment it below. You can also download Source Code used in this Article.


Attachments

  • Source Code (46045-5426934-Source-Code.zip)
  • Comments

    Author: chitaranjan mallick11 Mar 2015 Member Level: Gold   Points : 4

    Hi,

    u can pass parameter to particular Url like in mvc , suppose to pass parameter to action method that belonging to particular Controller like as follow:


    public static void RegisterRoutes(RouteCollection routes)
    {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );

    }

    hope it will help u out..

    Thnaks,
    Chitaranjan

    Author: Anoop Kumar Sharma11 Mar 2015 Member Level: Bronze   Points : 0

    Thanks @Chitaranjan for your feedback. I have already discussed the same thing in my First Article of ASP.NET MVC Series.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: