i have been stuck on few weeks, appreciated!
i building mvc5 web application (this first c# & asp.net project). model of application web service. there page checkbox, , when clicked, calls bit of jquery uses ajax call method in 1 of controllers. method calls web service , updates boolean value. seems working... issue need checkbox sent ajax call, can update label on page associated checkbox.
is there better way accomplish this? (it seems rather hack-ish me, using javascript call code). question, though, this: how can pass sender ajax call?
cshtml page:
@html.checkbox("checkbox_subscribe", new{@id = "subscribebox"}) @html.label("subscribebox", "please notify me via email of changes in lead times.") <script> $(document).ready(function () { $("#subscribebox").change(function (event) { $.ajax({ type: "post", url: "@url.action("subscribeclick", "home")", success: function (result) { alert(result); } }); }); }); </script>
controller method:
public string subscribeclick(object sender, eventargs e) { string flag; if (sender == checked) { flag = "y"; } else { flag = "n"; } websecurity.n_securitysoapclient proxy = new websecurity.n_securitysoapclient(); string result = proxy.setsubscribeflag("11", flag, "leadtimes"); if (result.startswith("<success>")) { if (flag == "y") result = "successfully subscribed email."; else result = "successfully unsubscribed email."; } return result; }
just side note: when try casting sender object checkbox object type, error: "invalidcastexception unhandled use code"
ajax client side code , has no concept of c# code such object sender, eventargs e
. change method accept boolean
[httppost] public actionresult subscribeclick(bool ischecked) { if(ischecked) { .... } else { .... } return json(result, }
and in script, pass true
or false
based on state of checkbox
$("#subscribebox").change(function (event) { var ischecked = $(this).is(':checked'); $.ajax({ type: "post", url: "@url.action("subscribeclick", "home")", data: { ischecked: ischecked }, success: function (result) { alert(result); } }); });