I want to update just the balance column, but I get an error:
An unhandled exception occurred while processing the request. SqlException:Cannot insert the value NULL into column 'IstanbulCardId', table 'Transportation.dbo.TbIstanbulCard'; column does not allow nulls. INSERT fails. The statement has been terminated
Tb Istanbul:
public partial class TbIstanbulCard
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IstanbulCardId { get; set; }
public decimal? Balance { get; set; }
public int CustomerId { get; set; }
public virtual TbCustomer Customer { get; set; }
}
VmIstanul:
public class VmIstanbul :TbIstanbulCard
{
public string CustomerType { get; set; }
public string CustomerName { get; set; }
public double TransportNumber { get; set; }
public double NumberOfStops { get; set; }
public string Vehicles { get; set; }
public double AvailableBalance { get; set; }
public double DeductedAmount { get; set; }
}
Action Controller:
public async Task<IActionResult> CalcIstanbul(VmIstanbul card)
{
if (card.Vehicles == "Bus")
{
customer.IstanbulBus(card);
Convert.ToDecimal(ViewBag.balance = customer.IstanbulBalance());
ViewBag.amount = customer.IstanbulAmountDeducted();
var update = new TbIstanbulCard()
{
IstanbulCardId = card.IstanbulCardId,
Balance = ViewBag.balance
};
using (var edit = ctx) {
ctx.TbIstanbulCards.Attach(update);
ctx.Entry(update).Property(x => x.Balance).IsModified = true;
ctx.SaveChanges();
}
}
else if (card.Vehicles == "MetroBus")
{
customer.IstanbulMetroBus(card);
ViewBag.balance = customer.IstanbulBalance();
ViewBag.amount = customer.IstanbulAmountDeducted();
}
return View();
}
View Istanbul:
@model Transportation.Vm.VmIstanbul
<div class="contact_section_2">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="input_main">
<div class="container">
<form asp-controller="Customer" asp-action="CalcIstanbul">
<div class="form-group">
<label for="departmentsDropdown"><b>Card</b></label>
<select asp-for="IstanbulCardId" style=" background: #ececec; border: none; color: #000; width: 100%; height: 55px; font-size: 22px; padding: 0px; border-radius: 10px; margin-top: 6px;" onchange="getValue()" id="departmentsDropdown" name="departmentsDropdown"></select><br />
</div>
<div class="form-group">
<label><b>Balance</b></label>
<input asp-for="Balance" class="email-bt" type="text" value="" id="show" />
</div>
<div class="form-group">
<label><b>Customer Type</b></label>
<input asp-for="CustomerType" class="email-bt" type="text" value="" id="sshow" />
</div>
<div class="form-group">
<select asp-for="Vehicles" id="service" style=" background: #ececec; border: none; color: #000; width: 100%; height: 55px; font-size: 22px; padding: 0px; border-radius: 10px; margin-top: 6px;">
<option>Select Vehicle Type</option>
<option value="MetroBus" data-target="devices" id="screen">MetroBus</option>
<option value="Bus" data-target="comp" id="comp">Bus</option>
</select>
</div>
<div class="form-group">
<div style="display:none" id="service-devices">
<select asp-for="NumberOfStops" id="devices" style=" background: #ececec; border: none; color: #000; width: 100%; height: 55px; font-size: 22px; padding: 0px; border-radius: 10px; margin-top: 6px;">
<option>Please Select The Number of Stops</option>
<option value="3">3</option>
<option value="9">9</option>
<option value="15">15</option>
<option value="21">21</option>
<option value="27">27</option>
<option value="33">33</option>
</select>
</div>
</div>
<div class="form-group">
<div style="display:none" id="service-comp">
<select asp-for="TransportNumber" id="compwork" style=" background: #ececec; border: none; color: #000; width: 100%; height: 55px; font-size: 22px; padding: 0px; border-radius: 10px; margin-top: 6px;">
<option>Please Select Your Transport Number</option>
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
</div>
<div class="send_btn">
<button type="submit" class="btn btn-success mr-1 mb-1" asp-controller="Customer" asp-action="CalcIstanbul">Calculate</button>
</div>
<div class="col-12">
<div class="form-group row">
<div class="col-md-2">
<span>Deducted Amount</span>
</div>
<div class="col-md-8">
<input class="form-control" placeholder="@ViewBag.amount">
</div>
</div>
</div>
<div class="col-12">
<div class="form-group row">
<div class="col-md-2">
<span>Available Balance</span>
</div>
<div class="col-md-8">
<input type="number" class="form-control" placeholder="@ViewBag.balance">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
@section Scripts{
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/Customer/SelectIstanbul",
success: function (data) {
console.log(data);
var s = '<option value="-1">Please Select a Card id</option>';
for (var i = 0; i < data.length; i++) {
s += '<option value="' + data[i].customerType + "," + data[i].balance + '">' + data[i].istanbulCardId + '</option>';
}
$("#departmentsDropdown").html(s);
}
})
})
function getValue() {
var myVal = $("#departmentsDropdown").val();
$("#show").val(myVal.split(",")[1]);
$("#sshow").val(myVal.split(",")[0]);
}
$(document).ready(function () {
$('select').on('change', function () {
var target = $(this).find(":selected").attr("data-target");
var id = $(this).attr("id");
$("div[id^='" + id + "']").hide();
$("#" + id + "-" + target).show();
});
});
</script>
}
question from:
https://stackoverflow.com/questions/65920429/sqlexceptioncannot-insert-the-value-null-into-column-istanbulcardid 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…