I have a knockout Observable which is populated during some interaction with my page. This Observable is a comma-separated value (i.e. "a,b,c"). What I need now is to pass this as an argument to @Url.Action. See below:
<input type="button" onclick="window.location.href='@Url.Action("Benefits", "Employee", new { ids = "a,b,c" })';" />
How can I do this?
You could use the click binding.
<button data-bind="click: function () { myFunction('@Url.Action("Benefits", "Employee")'); }">
Click me
</button>
//then in your view model:
self.myFunction = function (actionURL) {
window.location.href = (actionURL + '?ids=' + this.ids());
};
or just the attribute binding directly on a link
<a data-bind="attr: { 'href': '@Url.Action("Benefits", "Employee")?ids=' + ids() }">
Click
</a>
you will need to build your url as a query string manualy
suppose ids = ko.observable('a,b,c');
then you'll build your url
<input type="button" onclick="window.location.href='@(Url.Action("Benefits", "Employee"))' + '?ids=' + $data.ids()" />
that will generate a url like http://localhost:5248/Employee/Benefits?ids=a,b,c
or try this
<button data-bind="click: function() { window.location.href='@(Url.Action("Benefits", "Employee"))' + '?ids=' + ids() }">
Click me
</button>
JSFiddler DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With