Atta Kumah

Atta Kumah

  • NA
  • 167
  • 12.2k

How to Send Data to Database using Jquery

Jun 13 2020 12:19 PM
Plase can anyone help me out. Still trying to Figure out what is wrong that i can not send this data to my Database in mvc using Jquery. Th code is Below
 
Controller 
 
SensorDB Sned = new SensorDB();
public ActionResult Sendia()
{
return View();
}
[HttpPost]
public JsonResult Sendia( string sdy, string vri, string Anco, string am)
{
Senser sen = new Senser();
sen.Sendy = sdy;
sen.Vco = vri;
sen.AcBal = Anco;
sen.Amount = am;
Sned.Sensers.Add(sen);
Sned.SaveChanges();
return Json(JsonRequestBehavior.AllowGet);
}
 
View Action
 
@model Coder.Models.Senser
@{
ViewBag.Title = "Osmon";
}
<div style="width:20%; margin-top:5px">
<form id="myForm">
<span>Sendy</span> <span style="margin-left:40px"> @Html.TextBoxFor(model => model.Sendy, new { @class = "form-control", @placeholder = "Sendy" })</span>
<span>Vco</span> @Html.TextBoxFor(model => model.Vco, new { @class = "form-control", @placeholder = "Vco" })
<span>AcBal</span> @Html.TextBoxFor(model => model.AcBal, new { @class = "form-control", @placeholder = "AcBal" })
<span>Amount</span> @Html.TextBoxFor(model => model.Amount, new { @class = "form-control", @placeholder = "Amount" })
<br />
<input type="reset" value="Submit" class="btn btn-success" id="btnSave" />
</form>
</div>
<script>
$(function () {
$("#btnSave").click(function () {
var data = $("#myForm").serialize();
$.ajax({
type: "POST",
url: "/Codian/Sendia",
data: data,
success: function (response) {
alert("Successful")
},
error: function () {
alert("Failed! Pleased try again")
}
});
});
});
</script>
 
The result is always Failed! Pleased try again.
I do not know where i went wrong. 

Answers (2)