最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc - How to check model values in View Model from Javascript in MVC4 - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited May 23, 2013 at 8:18 tereško 58.5k25 gold badges100 silver badges150 bronze badges asked May 23, 2013 at 8:03 Ahammad FekriAhammad Fekri 312 silver badges5 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 2

There may be two way

first

@{
    string property = "";

    property = Model.Property == null ? "null value" : "value";
    // OR another parison 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>
发布评论

评论列表(0)

  1. 暂无评论