This article will show you how you can MVC Pass Multiple Parameters in QueryString or
URL in Asp.Net MVC using c#. In this you will learn to pass and access multiple
QueryString value in URL in asp.net mvc using c#.
Some of my previous articles are as follows: jQuery
Articles, Partial
view in Asp.Net MVC, jQuery
Ajax Search and Display In MVC WebGrid in Asp.Net MVC Using C#.Net, Search
and Display Data In MVC WebGrid in Asp.Net MVC Using C#.Net, Hyperlink
Adding In MVC WebGrid in Asp.Net MVC Using C#.Net.
So for this article first will create a new asp.net mvc application and add a controller file and in this we will add two action method. One for providing link and in which we will pass the value and other for accessing the request querystring value.
So for this article first will create a new asp.net mvc application and add a controller file and in this we will add two action method. One for providing link and in which we will pass the value and other for accessing the request querystring value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController
: Controller
{
//
// GET:
/Home/
public
ActionResult Index()
{
return
View();
}
public
ActionResult IndexOutput()
{
string
FirstName = Request.QueryString["FirstName"].ToString();
string
LastName = Request.QueryString["LastName"].ToString();
string
Marks = Request.QueryString["Marks"].ToString();
ViewData["FirstName"]
= FirstName;
ViewData["LastName"]
= LastName;
ViewData["Marks"]
= Marks;
return
View();
}
}
}
|
In above code I have passed the request QueryString by storing into ViewData to display in view. Now we will create the view and add the below code.
@{
ViewBag.Title = "MVC
Pass Multiple Parameters in QueryString or URL in Asp.Net MVC";
}
@using (Html.BeginForm("Index", "Home"))
{
<a href="Home/IndexOutput/?FirstName=John&LastName=Mathu&Marks=80%"><b>Click to
Redirect</b></a>
}
|
Second one.
@{
ViewBag.Title = "IndexOutput";
}
<h4>
MVC Pass Multiple Parameters in <br />QueryString or URL in Asp.Net MVC</h4>
<div>
First Name :@ViewData["FirstName"]<br />
Last Name :@ViewData["LastName"]<br />
Marks :@ViewData["Marks"]
</div>
|
Here we have displayed the value. Now we have done run the
application to check the output.
Now click on link. After this we will hit the break point. So
here check the we are getting request querystring value.
Here is the url
Here is the final output.
Good Post.
ReplyDeleteThank you Sir... you are realy help full.
Ok. But if you need to write a map to route to that URL:
ReplyDelete"Home/IndexOutput/?FirstName=John&LastName=Mathu&Marks=80%"
How would such a map definition look like?
TIA
Rick
Have a look of the code
Deleteroutes.MapRoute(
"ArtistImages", // Route name
"{controller}/{action}/{artistName}/{apikey}", // URL with parameters
new { controller = "Home", action = "Index", artistName = "", apikey = "" } // Parameter defaults
);
For detail check the links
http://stackoverflow.com/questions/2246481/routing-with-multiple-parameters-using-asp-net-mvc
http://stackoverflow.com/questions/19578408/multiple-route-with-multiple-parameters-mvc4