c# - Sending simple type from HTML form to ASP API -
i having troubles sending simple double number form asp api.
when submit form, firstly need remove submit button avoid sending redundant 'submit=submit' value api controller.
as have read in this article, set name of input field empty. if check http request in developer mode, body of sent data looks '=value'. method parameter decorated [frombody] attribute.
even this, controller method not value. allways zero.
what do?
my form looks so:
<form action="@url.action("temperature","api")" method="post" > <input type="number" name=" " step="0.25"/> <input type="submit" name="submit" /> </form> </div> <script> $("form").submit(function () { $(this).children('[name="submit"]').remove(); }); </script> the controller:
// post: api/temperature public void post([frombody]double value) //formdatacollection data { temperature newentry = new temperature() { timestamp = datetime.now, value = value}; try { db.temperatures.add(newentry); db.savechanges(); } catch(exception e) { debug.writeline(e.message); } }
try ajax...
change html little bit this:
<div> <input type="number" id="number" name=" " step="0.25"/> <input type="button" id="submit" name="submit" /> </div> then:
$(document).on('click', '#submit', function () { $.ajax({ type: 'post', url: '/controllername/post', data: { value: json.stringify($('#number').val()) }, success: function (data) { alert(data); }, error: function (data) { alert("an issue has occured"); } }); }) also controller input parameters like:
public void post([frombody]string value) //convert string double later within method
Comments
Post a Comment