Question

I assinged two values in button click. when the page is redirecting the 2 values will pass. But in page i don't know how to take a second value using query string..

<a href="<%: Url.Action("Index", "Test", new { planId = "500" ,validity="120"}) %>"><a>

<% var value = string.Empty;
      var value1 = string.Empty;
       if (Request.QueryString.Count > 0)
       {
           value = Request.QueryString[0];
           Session["PlanId"] = value;
       }
       else if (Request.QueryString.Count>0)
       {
           value1 = Request.QueryString[3];
           Session["Validity"] = value1;
       }%>

PlanId is taking the value. but validity is not taking the value.. Please help me how to do?

Was it helpful?

Solution

There is another way to solve this problem instead of using QueryString collection.

Say if you have URL like this -

<a href="@Url.Action("Test", "Model" , new { planid="RAT500" ,validity="120" })">hi<a>

Then have corresponding action like this -

public class ModelController : Controller
{
    public ActionResult Test(string planid, string validity)
    {
        return null;
    }
}

And when you click the link, your values will be there in the parameters as shown below -

enter image description here

And say you have other QueryStrings as shown below -

<a href="@Url.Action("Test", "Model" , new { planid="RAT500" ,validity="120", name = "rami"})">hi<a>

Then you can get other QueryString as shown below -

enter image description here

OTHER TIPS

It's an else if so if the first if is true, it'll skip else if. It should be as follows:

 <% var value = string.Empty;
    var value1 = string.Empty;
    if (Request.QueryString.Count > 0)
    {
        value = Request.QueryString[0];
        Session["PlanId"] = value;
    }

    if (Request.QueryString.Count > 3)
    {
        value1 = Request.QueryString[3];
        Session["Validity"] = value1;
    } %>

I'm not sure what the issue is, but the second test should be checking Count > 3, not Count > 0.

QueryString can be indexed by names directly - would be easier to read (and avoid getting elements outside of the string):

  var planId = Request.QueryString["planId"];      
  var validity = Request.QueryString["validity"];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top