3

I want to check that a Model value is NULL or NOT NULL in my view model in JavaScript, using mvc4.

How would I check this?

How do I get the values from my Model in JavaScript in the View model?

0

2 Answers 2

2

There may be two way

first

@{
    string property = "";

    property = Model.Property == null ? "null value" : "value";
    // OR another comparison strategy
    ...
}

<script type="javascript">
   var property = @(property);
</script>

second

<script type="javascript">
  var property = @(Model.Property == null ? "null value" : "value");
</script>
Sign up to request clarification or add additional context in comments.

Comments

0

This will send a model's property from MVC to JS (views with Razor syntax):

<script type="text/javascript">
  var property = @Model.Property;
</script>

Old syntax will be:

<script type="text/javascript">
  var property = <%=Model.Property%>;
</script>

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.