0

I have this URL

var url = "/test/Create/" + $("#hdnFlag").val() +"?CmpT="+"Ilim";
window.location.href = url;

and in my Test controller I do this to get query string value

 tM_PMO.Type = Request.QueryString["CmpT"];

But always give me null values.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
marco debala
  • 99
  • 1
  • 8

2 Answers2

0

There is a difference between the GET and POST types.

Query string can be read with the URL of the GET request. However, you cannot read the Query string value in the URL when you make a POST request. For this you need to submit it to the server.

Below I give you a few examples of usage.

GET Request You can read Query string with URL as below

public ActionResult Test(string CmpT)
{
    if (!string.IsNullOrWhiteSpace(CmpT))
    {
        //your codes...
    }else
    { }
    return View();
}

POST Request If you are making a POST request and trying to read from the URL, it will return null. In order to read it, you need to send this value to the server as follows.

1st way : In your Html.BeginForm in your View Below, submit Query string as below and read this value as Action parameter

View Page

@using (Html.BeginForm("Test", "XController", new { returnUrl = Request.QueryString["CmpT"] }, FormMethod.Post, new { role = "form" }))
{
    <button type="submit">Send</button>
}

Controller

public ActionResult Test(string returnUrl)
{
    if (!string.IsNullOrWhiteSpace(returnUrl))
    {
        //your codes...
    }else
    { }
    return View();
}

2nd way : Create a hidden form element as part of the form between the Html.BeginForm tags in your view page and give its value as a query string. Then call it in Action method like below.

View Page

@using (Html.BeginForm("Test", "XController", FormMethod.Post, new { role = "form" }))
{
    @Html.Hidden("returnUrl", Request.QueryString["CmpT"])
    <button type="submit">Send</button>
}

Controller

public ActionResult Test(string returnUrl)
{
    if (!string.IsNullOrWhiteSpace(returnUrl))
    {
        //your codes...
    }else
    { }
    return View();
}

or for multiple form items (You can also access other form elements this way)

public ActionResult Test(FormCollection fc)
{
    string _returnUrl = fc["returnUrl"];
    if (!string.IsNullOrWhiteSpace(_returnUrl))
    {
        //your codes...
    }else
    { }
    return View();
}
-1

I hope you are looking for below code sample where we just fetch the value in url which we says query string:

Request.QueryString["querystringparamname"].ToString(); 

You can assign this in any Var and use accordingly.

Singh Aswal
  • 379
  • 2
  • 6
  • Is it something wrong with the answer, i just replied becuase question was not clear that's i made this reply as seeing the question. – Singh Aswal Jun 18 '20 at 18:17
  • I didn't downvote your answer but I guess if OP is getting null value and you add a `.ToString()` by the end, it's going to raise an error [NullReferenceException](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it/4660186). Cannot call a method on a null reference object. – derloopkat Jul 22 '20 at 00:23