I have the following CSHTML view I’m calling to both display my “Create” and “Update” form.
All works fine till I decided to introduce the “if” condition to change both the title and the submit button captions of the form in order to differentiate between the “Create” and the “Update” action that is sent to my controller.
In the “if” statement, when I type the word “Model.” I can see the intelli-sense popping up my Model’s properties list that I can choose from.
When I build my project, everything looks fine.
But when I hit my submit button to show the “Create” form, I get the following error.
But when I hit my submit button to show the “Update” form, I don’t get any error.
I really don’t see the error or omission inside my code:
“
System.NullReferenceException: ‘Object reference not set to an instance of an object.’
“
(See screenshot)
@model ProfessionVM
<form method="post">
<div class="border p-3 mt-4 row">
<div class="row pb-2">
<h2 class="text-primary">@(Model.NoProfession != 0 ? "MODIFIER" : "AJOUTER") UNE PROFESSION</h2>
<hr />
</div>
<input asp-for="NoProfession" class="form-control" hidden />
<div class="mb-3 row p-1">
<label asp-for="NomProfession"> </label>
<input asp-for="NomProfession" class="form-control" id="txtNomProf"
onblur="validateInput(this)" />
<span asp-validation-for="NomProfession" class="text-bg-danger"> </span>
</div>
<br />
<div class="mb-3 row p-1">
<label asp-for="Inactif"> </label>
<input asp-for="Inactif" class="form-check-input" type="checkbox" id="chkInActif" checked
onblur="validateInput(this)" />
</div>
<br />
<div class="mb-3 row p-1">
<label asp-for="RemProfession"> </label>
<textarea asp-for="RemProfession" class="form-control" rows="5" id="txtRemProf"
onblur="validateInput(this)">RAS</textarea>
</div>
<div class="row">
<div class="col-6 col-md-3">
@if (Model.NoProfession != 0 || Model.NoProfession != null)
{
<button type="submit" class="btn btn-primary form-control" id="boutModif" disabled>Modifier</button>
}
else
{
<button type="submit" class="btn btn-primary form-control" id="boutModif" disabled>Ajouter</button>
}
</div>
<div class="col-6 col-md-3">
<a asp-controller="Administrateur" asp-action="list_Professions" class="btn btn-dark border form-control">
Retour ร la liste des professions
</a>
</div>
</div>
</div>
</form>
<input type="text" id="verifValNomProfession" value="0" hidden />
<input type="text" id="verifValRemProfession" value="0" hidden />
<input type="text" id="verifChkInActif" value="0" hidden />
@section Scripts {
@{
<partial name="_ValidationScriptsPartial" />
}
<script type="text/javascript">
$('form').submit(function(e) {
$(':disabled').each(function(e) {
$(this).removeAttr('disabled');
})
});
function validateInput(input) {
switch (input.id) {
case "txtNomProf":
case "txtRemProf":
var defaultVal = input.defaultValue.toLowerCase().replace(new RegExp(/\r?\n|\r/g), " ");
var currentVal = input.value.toLowerCase().replace(new RegExp(/\r?\n|\r/g), " ");
var c = document.getElementById("verifChkInActif");
var g = document.getElementById("boutModif");
if (input.id == "txtNomProf") {
var b = document.getElementById("verifValNomProfession");
var d = document.getElementById("verifValRemProfession");
} else {
var b = document.getElementById("verifValRemProfession");
var d = document.getElementById("verifValNomProfession");
};
(defaultVal == currentVal) ? b.value = "0" : b.value = "1";
(parseInt(b.value) + parseInt(c.value) + parseInt(d.value) > 0) ? g.disabled = false : g.disabled = true;
break;
case "chkInActif":
console.log("C'est bon!");
var x = input.defaultChecked;
var z = input.checked;
var b = document.getElementById("verifChkInActif");
var c = document.getElementById("verifValNomProfession");
var d = document.getElementById("verifValRemProfession");
var g = document.getElementById("boutModif");
(x.toString() == z.toString()) ? b.value = "0" : b.value = "1";
(parseInt(b.value) + parseInt(c.value) + parseInt(d.value) > 0) ? g.disabled = false : g.disabled = true;
break;
default:
alert ("Il y a gbangban!");
};
};
</script>
}