Gyuzal
Gyuzal

Reputation: 1591

Change route values of a link with jQuery

I'd appreciate any help.

I'm trying to change the link href each time with Kendo Grid change() event:

 function ContractsGrid_onChange(e) {
    var selected = this.select()[0],
        item = this.dataItem(selected);       

    $('#createOnBase').attr('href', function () {

        var createLink = document.getElementById("createOnBase");
        var route = 'http://' + createLink.hostname + createLink.pathname + "?contractID =" + item.ID; 

        return route;

    });
  }

 @Ajax.ActionLink("Create", "CreateOnBase", 
                  new { contractID = "_contractID_" }, 
                  new AjaxOptions() {... }, 
                  new { id="createOnBase" })

I'm not sure with current approach, because i have different hostnames (localhost with port or server domain)

The best way would be:

var route = "@Url.Action('CreateOnBase', new { contractID = ??})";

But I cannot use JS variable (item.ID) in razor.

Also, this.href.replace("_contractID_", item.ID) won't work for several changes.

Can you help me with another solution?

Thanks a lot!

Upvotes: 1

Views: 2981

Answers (2)

Ivan
Ivan

Reputation: 789

The first thought that come up to my mind is to store root url in separate variable.Something like this:

function ContractsGrid_onChange(e) {
    var selected = this.select()[0],
        item = this.dataItem(selected);       
    var rootUrl = @Url.Action("Create", "CreateOnBase");      
    $('#createOnBase').attr('href', function () {

        var createLink = document.getElementById("createOnBase");
        var route = rootUrl  + "?contractID =" + item.ID; 

        return route;

    });
  }

This is just a workaround, not sure about some advanced way...

Upvotes: 1

Gyuzal
Gyuzal

Reputation: 1591

Yeh, that was easy, I found a way:

  $('#createOnBase').attr('href', function () {

        return "@Url.Action("CreateOnBase")"+ "/?contractID="  +item.ID;

    });

maybe it will be helpful for someone.

Upvotes: 1

Related Questions