1

Good day,

I have an asp.net mvc4 project where I tried declare variable in jquery. Problem to be concluded in next: my data get from the model @Model.EducationProgramBachelorId and the property EducationProgramBachelorId in my class declared as int?. And when i try declare it in my jquery code:

var progId = @Model.EducationProgramBachelorId;

R# told me that int? University.EducationProgramBachelorId Syntax Error

I need this variable in my if/else condition where i want to do next:

if(progId != null){
//Do something
}
else{
//Do something
}

My questions is:

  1. How can I declare int? variable in jquery?

  2. In if statement when it's true I need write just return for that my function is closed or something else?

EDIT

Here is the part of my RAZOR page:

@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>
10
  • 2
    you don't need to declare a variable as an int in javascript. Where are you seeing the error? it doesn't look like a javascript error at all. Commented Sep 12, 2013 at 15:59
  • Is ReSharper telling you that there's an error, or is the compiler or runtime telling you that there's an error? ReSharper could be mistaken. Commented Sep 12, 2013 at 16:00
  • After this line of code var progId = @Model.EducationProgramBachelorId I have red wavy line where have my error Commented Sep 12, 2013 at 16:07
  • @BorHunter is that line in a .js file or an asp.net page? We need more information. It sounds to me like that should be in some javascript but it's not. Commented Sep 12, 2013 at 16:10
  • 1
    @BorHunter: I've had issues in the past where Visual Studio didn't understand a mix of Razor code and JavaScript code in a View. But that doesn't prevent it from actually working. Does the code compile? Does it run? Are there actual errors or just intellisense errors? Commented Sep 12, 2013 at 16:12

2 Answers 2

1

If a nullable int? property is rendered to the view, it will not output anything. Can you create a new property in your model that will convert the int? to a normal int? Then return a zero if the EducationProgramBachelorId variable is null.

public int EducationProgramBachelorJSInt
{
    get
    {
        return EducationProgramBachelorId.HasValue ? 
        EducationProgramBachelorId.Value : 0;
    }
}

Then your JQuery code will look like this.

var progId = @Model.EducationProgramBachelorJSInt;
if(progId != 0){
    //Do something
}
else{
    //Do something
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can try,

 var progId = @(Model.EducationProgramBachelorId.HasValue ? Model.EducationProgramBachelorId : 0);

Thus if Model.EducationProgramBachelorId is null its progId will be set to 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.