I'm trying to make a simple ASP.NET Core MVC application which updates / inserts / displays data from our database. I have models written like this (similar to the db fields):
public class INVOICE : ModelBase
{
[Key]
public decimal ID { get; set; }
public string CONTRACTID { get; set; }
public string ORDERID { get; set; }
public decimal? INVOICEAMOUNT { get; set; }
public string BARCODE { get; set; }
...
}
(with some more functionality like db update / insert)
I have one controller:
public class HomeController : Controller
{
// GET: HomeController
INVOICE invoice = CRUD.GetFirstOrDefault(new INVOICE(), @"WHERE ID IN (75693)");
public ActionResult Index()
{
return View(invoice);
}
[HttpPost]
public bool Update()
{
return invoice.Update();
}
}
And the Index.cshtml
:
<body>
<div class="form-group">
<label>Barcode</label>
<input type="text" class="form-control" id="BARCODE" placeholder="0" value="@Model.BARCODE" readonly>
</div>
<div class="form-group">
<label>Id</label>
<input type="text" class="form-control" id="ID" placeholder="0" value="@Model.ID" readonly>
</div>
<div class="form-group">
<label>INVOICEAMOUNT</label>
<input type="text" class="form-control" name="INVOICEAMOUNT" id="INVOICEAMOUNT" value="@Model.INVOICEAMOUNT" aria-describedby="emailHelp" placeholder="0">
</div>
<form id="formUpdate">
<div>
<asp:button id="Button" class="btn btn-primary">Update</asp:button>
</div>
</form>
@section Scripts{
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(function () {
$("#Button").click(function () {
$.ajax({
type: "POST",
url: "/Home/Update",
success: function (response) {
console.log(response);
},
failure: function (response) {
alert("Fail");
},
error: function (response) {
alert("error");
}
});
});
});
</script>
}
</body>
But now, when I'm clicking the button after I changed the Inputfield value of the INVOICEAMOUNT
, the update is called and successful, but the values are the same as they where when I initialized the model.
How do I tell my model that the values got changed?
Edit: My wording is bad. The Update is working, but the update isn't using the values that are displayed in the view. It's still using the values I initialized, even though I changed the input field values (clicked in it, wrote a new number).