Anuja
Anuja

Reputation: 97

How to pass Id value to window.location.href in MVC application

Currently I am working on MVC application in that I need to render page to another action in MVC application for that I need to pass Id parameter to that Action.

Here is my Code.

var CardCode=$('#CardCode').val();
if (str.substr("successfully")) {
    window.location.href='@Url.Action("EditPartner","MstPartner",new { id = CardCode})';
}

in that code when I am passing value to that id, i.e id=CardCode that is not allowed there then how to pass CardCode value to that action? Please give some suggestion.

Upvotes: 2

Views: 10404

Answers (2)

Ilya Sulimanov
Ilya Sulimanov

Reputation: 7836

If you want do it dynamic on client side that you should do it by means of javascript

function replaceUrlParam(url, paramName, paramValue){
    if(paramValue == null)
        paramValue = '';
    var pattern = new RegExp('\\b('+paramName+'=).*?(&|$)')
    if(url.search(pattern)>=0){
        return url.replace(pattern,'$1' + paramValue + '$2');
    }
    return url + (url.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue 
}

var url = '@Url.Action("EditPartner","MstPartner",new { id = CardCode})';
var newUrl = replaceUrlParam(url,"Id","OtherCardCode");

Upvotes: 0

too_cool
too_cool

Reputation: 1194

Your code doesn't work because you can't pass a variable in there. It only works if value is hardcoded

window.location.href='@Url.Action("EditPartner","MstPartner",new { id = "123"})';

What you can do is use a Replace method

var CardCode=$('#CardCode').val();
if (str.substr("successfully")) {
    window.location.href='@Url.Action("EditPartner","MstPartner",new { id = "CC"})'.replace("CC",CardCode);
}

Upvotes: 1

Related Questions