使用Form表单提交到数据库
这两个必须要写,不能写这样的格式,会提交获取不到数据
contentType: ‘application/json‘,
正确格式:
contentType: ‘application/x-www-form-urlencoded‘,
accepts:‘application/x-www-form-urlencoded‘,
Api代码
[EnableCors("any")] [Route("insert")] [HttpPost] public int Add([FromForm] ProductInfoModel model) { try { int h = _pbll.Add(model); if (h > 0) { return 1;//添加成功; } else { return 0;//登录失败; } } catch (Exception) { throw; } }
cshtml代码
<div> <br> <div> <table> <tr> <td>产品编号</td> <td><input type="text" id="product_No" placeholder="请输入编号"></td> <td></td> <td></td> </tr> <tr> <td>最低申请额</td> <td> <input type="text" id="minAmount" placeholder="最低申请额"> </td> <td>最高申请额</td> <td> <input type="text" id="maxAmount" placeholder="最低申请额"> </td> </tr> <tr> <td>最低期限</td> <td> <input type="text" id="minTerm" placeholder="最低期限"> </td> <td>最高期限</td> <td> <input type="text" id="maxTerm" placeholder="最高期限"> </td> </tr> <tr> <td></td> <td> <button type="button" id="btn_save">立即提交</button> <button type="button" id="btn_reset">重置</button> </td> <td></td> <td></td> </tr> </table> </div> </div> <script> //添加 $(‘#btn_save‘).click(function () { var obj = { "Product_No": $(‘#product_No‘).val(), "MinAmount": $(‘#minAmount‘).val(), "MaxAmoun": $(‘#maxAmount‘).val(), "MinTerm": $(‘#minTerm‘).val(), "MaxTerm": $(‘#maxTerm‘).val() }; $.ajax({ url: ‘http://localhost:52742/Product/insert‘, type: ‘POST‘, dataType: ‘json‘, contentType: ‘application/x-www-form-urlencoded‘, accepts:‘application/x-www-form-urlencoded‘, data: obj, success: function (data) { if (data > 0) { alert("添加成功"); window.location.href = "/Loan/Index"; } } }) }) </script>