javascript - The name <js_variable> does not exist in the current context -
i have following model set there list<string>
trying add value to:
public class newshipmentmodel { ... public list<string> units_selected { get; set; } public newshipmentmodel() { units_selected = new list<string>(); } }
in view, have eventlistener set on checkbox trying take value of checkbox , either add or remove list using javascript , razor:
(var = 0; < unitcheckboxes.length; i++) { unitcheckboxes[i].addeventlistener("change", function () { var addition = 0; var unit_serial = this.value; var curr_val = (this.name == "base") ? parseint($("#total-bases-selected").text()) : parseint($("#total-clients-selected").text()); if (this.checked) { addition = 1; @model.units_selected.add(unit_serial); // error here } else { addition = -1; @model.units_selected.remove(unit_serial); // error here } curr_val = curr_val + addition; if (this.name == "base") $("#total-bases-selected").text(curr_val); else $("#total-clients-selected").text(curr_val); }, false); }
however, trying add or remove value fails have marked error here
in comments. issue cannot access unit_serial
seems out of scope razor:
the name unit_serial not exist in current context
how access variable? possible?
edit: note data passed controller via submit
button , http post. i'm not sure how populate list and/or inject data in rest of form.
it seams you're confusing server , client code. razor part executes on server. javascript - on client. therefore can't
//note unit_serial doesn't exist when you're calling add method because //executes during razor view generation on server. //all javascript code executes after client receive generated view. @model.units_selected.add(unit_serial);
in middle of javascript code, because doesn't make sense. when change event handler fires on checkboxes: send server updating.
for making work consider create 1 more mvc action , use ajax passing data server.
Comments
Post a Comment