This sytax is in razor that I am converting to knockoutJS.. any help much appreciated..
Where ItemStatus.Active is 0 in an enum in c# ( backend )
@foreach (var employee in Employees.Where(x => x.Status == ItemStatus.Active))
{
<div class="someclass”>
<span class="label">Name:</span>
<span class="value">@employee.Name</span>
</div>
}
How do I replicate this logic in knockout foreach, ie I only want to show employees who are currently active or employed
Thank you
Razor runs server-side. So, you can't use
Employees
with knockout bindings which are apllied client-side. You need to hand over thisEmployees
data to a javascript variable, and then bind that using knockout which will run client-side, after the page loads.Use the
foreach
binding in your HTML like this (HereEmployees
refers to the property of your knockoutviewModel
, not MVC)You also need to add
@using Newtonsoft.Json
to the top yourcshtml
file forJsonConvert
Any JavaScript expression can be used in Knockout bindings.
So you could use the filter function (ES6):
Or any other method of filtering: