I am developing an ASP.NET Core project. I am using MSSQL as a database. In my database, there is a Explanation column in my table named Inventory and this column can be left nullable. But I get an error when I leave this field blank while adding data through the project.
FluentValidation – InventoryAdditionValidator.cs,
public class InventoryAdditionValidator : AbstractValidator<CreateInventoryDto>
{
public InventoryAdditionValidator()
{
RuleFor(x => x.InventoryID).NotEmpty().WithMessage("Demirbaş No Boş Bırakılamaz");
RuleFor(x => x.Brand).NotEmpty().WithMessage("Marka Boş Bırakılamaz");
RuleFor(x => x.Model).NotEmpty().WithMessage("Model Boş Bırakılamaz");
RuleFor(x => x.Type).NotEmpty().WithMessage("Demirbaş Tipi Boş Bırakılamaz");
RuleFor(x => x.SerialNo).NotEmpty().WithMessage("Seri Numarası Boş Bırakılamaz");
RuleFor(x => x.UsageCondition).NotEmpty().WithMessage("Kullanım Durumu Boş Bırakılamaz");
RuleFor(x => x.Condition).NotEmpty().WithMessage("Demirbaş Durumu Boş Bırakılamaz");
RuleFor(x => x.Ownership).NotEmpty().WithMessage("Mülkiyet Durumu Boş Bırakılamaz");
}
}
InventoryController.cs,
[HttpPost]
public async Task<IActionResult> AddInventory(CreateInventoryDto createInventoryDto)
{
var user = await _userManager.FindByNameAsync(User.Identity.Name);
var id = user.Id;
InventoryAdditionValidator validationRules = new InventoryAdditionValidator();
ValidationResult validationResult = validationRules.Validate(createInventoryDto);
if (validationResult.IsValid)
{
createInventoryDto.CreateUser = id;
createInventoryDto.CreateDate = DateTime.Now;
var client = _httpClientFactory.CreateClient();
var jsonData = JsonConvert.SerializeObject(createInventoryDto);
StringContent stringContent = new StringContent(jsonData, Encoding.UTF8, "application/json");
var responseMessage = await client.PostAsync("https://localhost:7198/api/Inventory", stringContent);
if (responseMessage.IsSuccessStatusCode)
{
return Json(createInventoryDto);
}
else
{
return BadRequest();
}
}
else
{
var errors = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
return BadRequest(new { Errors = errors });
}
}
InventoryAddition.cshtml – Ajax code,
<script>
$(document).ready(function () {
$("#btnAddInventory").click(function () {
let value = {
InventoryID: $("#input-qr-result").val(),
Brand: $("#choices-single-brand").val(),
Model: $("#txtModel").val(),
Type: $("#choices-single-type").val(),
SerialNo: $("#txtSerialNo").val(),
UsageCondition: $("#choices-single-usage").val(),
Condition: $("#choices-single-condition").val(),
Ownership: $("#choices-single-ownership").val(),
Explanation: $("#txtExplanation").val()
}
$.ajax({
type: "POST",
url: "/Inventory/AddInventory",
data: value,
success: function () {
$("#order-tab").removeClass("active");
$("#order-tab-pane").removeClass("show active");
$("#delivery-tab").addClass("active");
$("#delivery-tab-pane").addClass("show active");
setTimeout(function () {
window.location.href = "/Inventory/Index";
}, 3000);
},
error: function (xhr) {
var errors = xhr.responseJSON && xhr.responseJSON.errors;
if (!errors) {
var errorMessage = xhr.responseText;
showToast(errorMessage);
} else {
showValidationErrors(errors);
}
}
});
});
function showValidationErrors(errors) {
if (Array.isArray(errors) && errors.length > 0) {
errors.forEach((error) => {
showToast(error);
});
}
}
function showToast(error) {
Toastify({
text: error,
duration: 3000,
gravity: "top",
position: 'right',
backgroundColor: "#f64e60",
stopOnFocus: true,
}).showToast();
}
});
</script>
CreateInventoryDto.cs,
public class CreateInventoryDto
{
public int InventoryID { get; set; }
public int Brand { get; set; }
public string Model { get; set; }
public int Type { get; set; }
public string SerialNo { get; set; }
public int UsageCondition { get; set; }
public int Condition { get; set; }
public int Ownership { get; set; }
public int CreateUser { get; set; }
public DateTime CreateDate { get; set; }
public string Explanation { get; set; }
}
I don’t check this field with Fluent validation, how can I solve this problem?
Thanks,
Kind regards.