te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>asp.net mvc 3 - Javascript within Razor view engine - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net mvc 3 - Javascript within Razor view engine - Stack Overflow

programmeradmin3浏览0评论

I have the following javascript:

<script type="text/javascript">
    function doReveal() {
        if (@Model.C1 == true) {
            document.getElementById('A1').style.backgroundColor = 'yellow';
        }
        if (@Model.C1 == false) {
            document.getElementById('A1').style.backgroundColor = 'yellow';
        }
</script>

I wanted to make it so that the function doReveal would check the Model.C1 (which is a boolean) and then change the background color if it was true.

However I get a message saying "conditional pilation is turned off" and a green squiggle under the Model.C1 and also the background color never changes even when I have both a true and false.

I tested out the background change line and that works outside of the "if's".

Looking at the page source I see this:

if (True == true) document.getElementById('A1').style.backgroundColor = 'yellow';

I changed it and removed the ==true but it still does not work. Even the following does not work. Yet it works is I remove the "if (True)" and just set the color.

if (True) document.getElementById('A1').style.backgroundColor = 'yellow';

Does anyone know what might be wrong?

I have the following javascript:

<script type="text/javascript">
    function doReveal() {
        if (@Model.C1 == true) {
            document.getElementById('A1').style.backgroundColor = 'yellow';
        }
        if (@Model.C1 == false) {
            document.getElementById('A1').style.backgroundColor = 'yellow';
        }
</script>

I wanted to make it so that the function doReveal would check the Model.C1 (which is a boolean) and then change the background color if it was true.

However I get a message saying "conditional pilation is turned off" and a green squiggle under the Model.C1 and also the background color never changes even when I have both a true and false.

I tested out the background change line and that works outside of the "if's".

Looking at the page source I see this:

if (True == true) document.getElementById('A1').style.backgroundColor = 'yellow';

I changed it and removed the ==true but it still does not work. Even the following does not work. Yet it works is I remove the "if (True)" and just set the color.

if (True) document.getElementById('A1').style.backgroundColor = 'yellow';

Does anyone know what might be wrong?

Share Improve this question edited Apr 10, 2011 at 18:07 Melissa asked Apr 10, 2011 at 17:55 MelissaMelissa 311 gold badge1 silver badge3 bronze badges 4
  • 2 You know the javascript runs client-side, after Razor and ASP/MVC, which run server side? – Dykam Commented Apr 10, 2011 at 18:09
  • In general, writing == true and == false in your C# code is the sign of a newbie, so try not to do that :) – Domenic Commented Apr 10, 2011 at 18:21
  • In C#, if (SomeBoolVar == true) can be written as if (SomeBoolVar) and if (SomeBoolVar == false) can be written as if (!SomeBoolVar ). The later ones are written with less number of characters and are more readable if the boolean variable is named properly by prefixing is, has etc. – IsmailS Commented Apr 29, 2011 at 7:42
  • 1 You know you are doing the same thing in the event of C1 being true or false? – Fenton Commented Aug 15, 2011 at 15:04
Add a ment  | 

2 Answers 2

Reset to default 11

The razor code starter sign must be in front of if statement. Otherwise, engine thinks that if is part of the javascript code, not the razor one. Also i think you will need razor text markers

      <script type="text/javascript">
            function doReveal() {
                @if (Model.C1 == true) {
<text>
                    document.getElementById('A1').style.backgroundColor = 'yellow';
</text>
                }
                @if (Model.C1 == false) {
                    document.getElementById('A1').style.backgroundColor = 'yellow';
                }
        </script>

Another case is when you want to run if statement in javascript and not the server side. In this case your razor markup is corect, but code generated would be like

    <script type="text/javascript">

    function doReveal() {

        if (False == true) {

            document.getElementById('A1').style.backgroundColor = 'yellow';

        }

        if (False == false) {

            document.getElementById('A1').style.backgroundColor = 'yellow';

        }

</script>

False is not equal to true, neither is equal to false. (javascript is case sensitive). Same case for True. So, your if statement won't execute.

It's important to realize that something like this will not make your page dynamically change background colors in response to changing values of the model, because ASP.NET runs on the server during the construction of the page, while JavaScript runs on the browser after the page is constructed and downloaded.

With that in mind, if you just want to change the background color that appears on page load, the following is probably a better bet:

@if (Model.C1) {
    <div id="A1" style="background-color: yellow;"></div>
} else {
    <div id="A1" style="background-color: blue;"></div>
}
发布评论

评论列表(0)

  1. 暂无评论