简体   繁体   中英

How to check model values in View Model from Javascript in MVC4

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?

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>

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM