How to post data to a controller method with Jquery Ajax that is in an external file? -
i trying use post data database ajax have in external file. know works in view
function submitclientinformationdata() { var datatosubmit = clientinformationdata(); $.ajax({ type: "post", url: "@url.action('addclientinformation')", datatype: "json", contenttype: "application/json; charset=utf-8", data: json.stringify(datatosubmit), success: function (data) { }, complete: function (e) { } }); }
but when add external js file, gets called never reaches controller method. took @ this , tried mentioned in here, button instead , looks this
<input id="btnsaveclientinfo" type="button" data-save-action-url="@url.action("addclientinformation")" value="save" class="btn btn-primary" />
but never got method have in controller view.
so in short, have ajax call in external file , have button in view , when click button data gets external file want call method in view's controller send data database. here basic of method trying call controller
[httppost] public actionresult addclientinformation(clientinformation clientinformation) { #region initialization oqoe = new oqoedal(); #endregion return view("preferences"); }
the code work if make ajax call view. if want work js file, you'll have following -
add following view -
<script type="text/javascript"> var addclienturlparam = { addclienturl : '@url.action("addclientinformation")' } </script> <script type="text/javascript" src="<location of js file>"></script>
make sure reference js file below script
block , change way make ajax call -
function submitclientinformationdata() { var datatosubmit = clientinformationdata(); $.ajax({ type: "post", url: addclienturlparam.addclienturl, datatype: "json", contenttype: "application/json; charset=utf-8", data: json.stringify(datatosubmit), success: function (data) { }, complete: function (e) { } }); }
Comments
Post a Comment