I work on asp.net mvc 5 I face issue when property SpeakStuff null it display error on console of browser tell me property SpeakStuff null so How to prevent error on console when property SpeakStuff is null how to handle it .
How to avoid error null when submit .
wht i try as below
TypeScript
Approve
java script function for submit
function submit() {
console.log("check submit");
var ResignationRequester = new Object();
ResignationRequester.SpeakStuff = document.getElementById('SpeakStuff').checked;
}
csharp main model i will submit
public class ResignationRequester
{
public bool SpeakStuff { get; set; }
}
controller action catched when update data and submit
[ValidateAntiForgeryToken]
public async Task ApprovalIndex(ResignationRequester REQ)
{
}
on asp.net mvc view sometimes session not detected or catch property Model.SpeakStuff so it display null
@if (Session[BLL.Common.SessionKeys.RoleCode].ToString() == "LM" || Session[BLL.Common.SessionKeys.RoleCode].ToString() == "LDM" || Session[BLL.Common.SessionKeys.RoleCode].ToString() == "DM")
{
Questions For Manager Before Approving:
if (Model.DirectManagerStatus == "Approve")
{
@Html.Label("Did You Meet/Speak to Staff", htmlAttributes: new { @class = "control-label col-md-5" })
Yes
No
$('.speak-stuff-checkbox').on('change', function () {
$('.speak-stuff-checkbox').not(this).prop('checked', false);
var selectedValue = $(this).val();
$('#SpeakStuff').val(selectedValue);
});
Amit MohantyRank #112 · 20K reputationPosted Nov 16, 2023, 5:52 AM EST
Inside your JavaScript
submit()function, ensuredocument.getElementById('SpeakStuff')is not null before attempting to access its properties.function submit() { console.log("check submit"); var speakStuffElement = document.getElementById('SpeakStuff'); if (speakStuffElement !== null) { var ResignationRequester = new Object(); ResignationRequester.SpeakStuff = speakStuffElement.checked; } else { console.error("SpeakStuff element not found or null!"); } }Or while preparing the object for submission, ensure that the property is set to a default value if it's null.
var ResignationRequester = new Object(); ResignationRequester.SpeakStuff = document.getElementById('SpeakStuff').checked ?? false;