[英]Jquery declare int? variable in asp.net mvc4
美好的一天,
我有一個asp.net mvc4項目,在其中嘗試在jquery中聲明變量。 接下來要得出的問題是:我的數據來自於我的類中聲明為int?
的模型@Model.EducationProgramBachelorId
和屬性EducationProgramBachelorId
int?
。 當我嘗試在我的jquery代碼中聲明它時:
var progId = @Model.EducationProgramBachelorId;
R#告訴我int? University.EducationProgramBachelorId Syntax Error
int? University.EducationProgramBachelorId Syntax Error
我想在接下來的if/else
條件下使用此變量:
if(progId != null){
//Do something
}
else{
//Do something
}
我的問題是:
如何聲明int?
jQuery中的變量?
在if
語句時,它是true
我需要寫只是return
了我的功能被關閉或其他什么東西?
編輯
這是我的RAZOR頁面的一部分:
@model Models.Entities.University
@Html.DropDownList("EducationProgramBachelorId", String.Empty)
@Html.ValidationMessageFor(model => model.EducationProgramBachelorId)
<script type="text/jscript">
$(document).ready(function () {
var progId = @Model.EducationProgramBachelorId; //here is I have an error and function didn't work
if(progId != null){
return;
}
else{
$.getJSON('/Administrator/ProgramList/' + 1, function (data) {
var items = '<option>Select a Program</option>';
$.each(data, function (i, program) {
items += "<option value='" + program.Value + "'>" + program.Text + "</option>";
});
$('#EducationProgramBachelorId').html(items);
});
}
});
</script>
如果可以為null的int?
屬性呈現給視圖,它將不會輸出任何內容。 您可以在模型中創建一個將轉換int?
的新屬性int?
正常的int? 如果EducationProgramBachelorId變量為null,則返回零。
public int EducationProgramBachelorJSInt
{
get
{
return EducationProgramBachelorId.HasValue ?
EducationProgramBachelorId.Value : 0;
}
}
然后,您的JQuery代碼將如下所示。
var progId = @Model.EducationProgramBachelorJSInt;
if(progId != 0){
//Do something
}
else{
//Do something
}
你可以試試,
var progId = @(Model.EducationProgramBachelorId.HasValue ? Model.EducationProgramBachelorId : 0);
因此,如果Model.EducationProgramBachelorId
為null,則其progId
將設置為0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.