1

I want to use javascript variable inside MVC Action route values. I referred this Stackoverflow post and the answer given there is working fine.

But i don't want to write an extra javascript function to achieve this. Without writing extra function, Is there any other way to do it. I meant, is there any new added feature in MVC4 for this? As the example in that link is for MVC 2.

self.EditUrl = ko.computed(function () {
            return "@Url.Action(Actions.User_Update, Controllers.User, new { Id = self.Id() } )";
        });

1 Answer 1

2

It is still the same case with MVC 4. You cannot mix client code and server code. I don't think it would even be possible in the future. Having said that, what you're trying to do is achievable. You can always write the url in a hidden field:

<input type="hidden" id="userUpdateUrl" value="@Url.Action("User_Update","User")"/>

Then use that on your client-side binding:

self.EditUrl = ko.computed(function () {
    return $("#userUpdateUrl").val() + "?" + self.Id();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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