Dever
Dever

Reputation: 3

Passing value from QueryString into Index action of controller in asp.net mvc

I have a querystring like this:

http://localhost:2563/Skill?Months=1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth=8&Year=2016,2017,2018&SelectedYear=2016&....

And I want to pass Months, SelectedMonth, Year, SelectedYear value into Index() of controller (Index is a function takes 0 argument). And another issue, after Index function completed, I want binding function (in javascript) to run to bind value into dropdownlist by the SelectedMonth, SelectedYear in querystring

Please help. This function helps access the Views by QueryString (not through my website) Many thanks.

Upvotes: 0

Views: 958

Answers (3)

First, the action name is wrong. you should use the following,

http://localhost:2563/index?Months=1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth=8&Year=2016,2017,2018&SelectedYear=2016&...

instead of

http://localhost:2563/Skill?Months=1,2,3,4,5,6,7,8,9,10,11,12&SelectedMonth=8&Year=2016,2017,2018&SelectedYear=2016&....

second, You need to pass some parameters. Here is how:

public ActionResult Index(List<int> Months,int SelectedMonth,List<int> Year, int Year)
{

}

remember to pass values as you wish to work with them. if you don't, you will face some error. use try catch block to prevent and handle exceptions.

You may also face exception accessing the web page. try to put optional parameter instead of using the above one.

 public ActionResult Index(List<int>? Months,int? SelectedMonth,List<int>? Year, int? Year)
    {

    }

Upvotes: 1

Konstantin Ershov
Konstantin Ershov

Reputation: 730

You need to get parameters from here HttpContext.Current.Request.QueryString

Upvotes: 0

shady youssery
shady youssery

Reputation: 440

public ActionResult Index(List<int> Months,int SelectedMonth,List<int> Year, int Year)
{

}

or use Reqest.QueryString

public ActionResult Index()
{
    var months=Reqest.QueryString["Months"];
    .
    .
    .

}

Upvotes: 0

Related Questions