Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a Knockout ObservableArray as an argument to ASP.NET MVC Razor @Url.Action

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?

like image 707
g_b Avatar asked Jan 17 '26 02:01

g_b


2 Answers

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>
like image 134
reckface Avatar answered Jan 19 '26 20:01

reckface


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

like image 43
Nerdroid Avatar answered Jan 19 '26 20:01

Nerdroid