[英]How to search for nulls in javascript
I keep on getting the error : "Value cannot be null" even if i'm checking for nulls. 我不断收到错误消息:即使我正在检查null,“值不能为null”。 This is part of my code:
这是我的代码的一部分:
<script type="text/javascript">
if (typeof @Model.Project.ProjectPricings.Count() != 'undefined' &&
@Model.Project.ProjectPricings.Count())
{
numberOfPricings = @Model.Project.ProjectPricings.Count();
}
sourceCodeList = '@Html.Raw(string.Join("", Model.SourceCodeList.Select(x => "<option></option><option value=" + x.SourceCode + ">" + x.SourceCode + "</option>")))';
targetCodeList = '@Html.Raw(string.Join("", Model.TargetCodeList.Select(x => "<option value="+ x.TargetCode + ">" + x.TargetCode + "</option>")))';
unitTypeCodeList = '@Html.Raw(string.Join("", Model.UnitTypeCodeList.Select(x => "<option value="+ x.UnitTypeCode + ">" + x.UnitTypeCode + "</option>")))';
currencyList = '@Html.Raw(string.Join("", Model.CurrencyList.Select(x => "<option value="+ x.CurrencyCode + ">" + x.CurrencyCode + "</option>")))';
serviceCodeList = '@Html.Raw(string.Join("", Model.ServiceCodeList.Select(x => "<option value="+ x.ServiceCode + ">" + x.ServiceCode + "</option>")))';
frequencyList = '@Html.Raw(string.Join("", Model.VolumeFrequencyList.Select(x => "<option value="+ x.VolumeFrequencyDescription + ">" + x.VolumeFrequencyDescription + "</option>")))';
@*$(document)
.ready(function() {
if (@Model.Project.Id == null) {
addPricing();
}
});*@
$('#submit-price-button').button();
$(".date").datepicker();
$(".combobox").SumoSelect({ search: true, searchText: 'Enter here.' });
</script>
By your description and the comment, my understanding is that you are having trouble when checking where @Model.Project.ProjectPricings.Count()
has value, if has value set it. 根据您的描述和评论,我的理解是,在检查
@Model.Project.ProjectPricings.Count()
值(如果已设置值@Model.Project.ProjectPricings.Count()
时遇到麻烦。
your problem is you are mixing razor and javascript code for validating - in your case, it seems logical to check directly on the server side (hence razor) 您的问题是您将剃刀和javascript代码混合在一起进行验证-在您的情况下,直接在服务器端进行检查似乎合乎逻辑(因此剃刀)
so change your code to: 因此将代码更改为:
@if (Model.Project.ProjectPricings != null)
{
@:numberOfPricings = @Model.Project.ProjectPricings.Count();
}
// rest same
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.