In cshtml, I have a @Model EnrollmentModel.
EnrollmentModel has LocationID field.
When I use the statement below, it works when the locationID exists. However, when the locationID is null, it gives an error.
var params = {LocationID : @Model.LocationID};
When I debug the javascript, it just says var params = {LocationID: };
I need a way for cshtml to convert @Model.LocationID to 0 when the value is null.
I also tried using @Model.LocationID ?? 0, but this converts into {blank} ?? 0 in javascript.
Please help.
the easiest way is to add a hidden field
<input type="hidden" value="@Model.LocationID" id="locationId" />
javascript
var params = { LocationID : 0};
var locationId=$("#locaionId").val();
if (locationId > 0) params.LocationID = locationId;
....