0

I am developing an ASP.Net MVC 3 Web Application. I have a Razor View which enables the User to Create a new entry in the database. Within this Create View, there is a Drop Down List and when the User selects an option the following JQuery code fires

$(document).ready(function () {

$("#gradeID").change(ChangeEventOfDDL);

function ChangeEventOfDDL() {
    var dropDownValue = $('#gradeID').val();
    //alert(dropDownValue);
    $.ajax({ type: "POST",
        url: '/FormEmployment/CreateSpecialtiesPartialView/' + dropDownValue,
        success: function (data) {
            $('#SpecialtiesContent').html(data);
        }
    });
}

});

As you can see within this code I retrieve the selected Drop Down List value using

var dropDownValue = $('#gradeID').val();

When a User is Editing an entry, I wish to fire off a similar piece of code, only this time I would like to retrieve the entry ID in the QueryString which will look something like this

http://localhost:56354/FormEmployment/Edit/41

Does anyone know how I can get the value 41 using JQuery or is this even possible?

Thanks for your help.

Tony.

2 Answers 2

2

If you can make an assumption that the value you want to get is always after the last slash in the url, you can try:

location.pathname.substring(location.pathname.lastIndexOf("/") + 1)

By using location.pathname, you don't have to worry about query string parameters, so the url could be http://localhost:56354/FormEmployment/Edit/41?foo=bar and it will still work as intended.

Sign up to request clarification or add additional context in comments.

Comments

2

The value that you're after in your example is actually part of the path, not part of a querystring.

If you do actually want to get a value from the querystring, you can do something like this:

getParameterByName: function (name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return decodeURIComponent(results[1].replace(/\+/g, " "));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.