c# - Call JavaScript function with Razor syntax -
c# - Call JavaScript function with Razor syntax -
i working on asp.net mvc application. database, have created 3 dictionaries , stored them within view model.
after populating said dictionaries logic in controller, i'm accessing these dictionaries in view via @model
after manipulating these dictionaries, need send info js function apply logic js library called dhtmlxscheduler
i have read razor syntax using @:
, using method send 2 string arrays. when trying access these arrays in js function, appear hold system.string[]
here code:
var weekarray; var engarray; @foreach(var week in model.weeklyschedule) { var key = week.key; var values = week.value.toarray(); var eng = model.engineerschedule.where(k => k.key == key).select(v => v.value).toarray(); string test = "hello"; @: gathertimes('@values', '@eng'); } function gathertimes(values, engs) { for(var = 0; < values.length; i++) { alert(values[i]); } //alert(values); //alert(engs); //weekarray[weekarray.length] = values; //engarray[engarray.length] = engineers; }
i plan on looping through arrays, hold info time slots , engineer ids. if there more info may provide, best so.
you need encode list json using json.encode. here example:
@{ // c# collection var collection= new list<string> {"a", "b", "c"}; } <script type="text/javascript"> // encode c# collection json, set javascript variable var arr = @html.raw(json.encode(collection)) for(var = 0; < arr.length; i++) { console.log(arr[i]); // output b c } </script>
in case so:
@: gathertimes('@html.raw(json.encode(values))', '@html.raw(json.encode(eng))');
javascript c# asp.net-mvc razor
Comments
Post a Comment