get QueryString value in MVC View

Get QueryString value in MVC View, JavaScript

Make a hidden field and put the vale in it as like below example, then get the value using jQuery selector.

As an Example,

MVC View Code looks like this,
<input type='hidden'  value='@Request.QueryString["token"]'  id='hdnToken' />

JavaScript code looks like this,
$(function () {
    var token$("#hdnToken").val();
    console.log(token);
});



Another Example,
//You can use URLSearchParams which is simple .
var getURLSearchParams = function (parameterName) {
    var result = null,
        tmp = [];

        location.search
        .substr(1)
        .split("&")
        .forEach(function (item) {
            tmp = item.split("=");
            if (tmp[0] === parameterNameresult = decodeURIComponent(tmp[1]);
        });

    return result;
}

//Usage, // query string: ?param1=anil&param2=&singh
let param1 = getURLSearchParams("param1");


ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^