如何比较TempData中的值?

时间:2013-12-25 11:41:54

标签: jquery asp.net-mvc

我从控制器接收 TempData 值。我在视图中打印它们,但是当我尝试比较它们中的任何一个时,它会显示语法错误,直到我在比较之前输入警告 ... 无错误代码版本:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
  }
<script>
        var i =  @TempData["count"] 

        alert("value of i is as bellow :" + i);
       if (i == 9)
       {

           $('#book_selection').attr("disabled", "disabled");
           alert("disabled...");
       }
</script>

但是当我删除提示时,它会在 if 语句中显示错误。如何解决?

这里是由html页面生成的代码:

<html>
    <head>
<title>
    Peace Quran
</title>

<style>
h1{font:25px arial,sans-serif;}
</style>
        <script type="text/javascript" src="/Scripts/jquery-1.7.1.min.js"></script>
</head>
<body>
      <div id="outer_div" style="background-color:Gainsboro; position:relative; top:50px; left:50px;height:550px;border-radius:8px; border:groove; width:1240px">

      <div id="header" style="background-color:Khaki    ; position:relative; top:5px; left:5px;height:50px;border-radius:8px; border:groove; width:1225px">
      <h1 style="left:550px; position:relative; top:-7px">Admin Panel</h1>
      </div> <!-- header ends-->

      <div id="lef-container" style="background-color:LightSteelBlue    ; position:absolute; top:65px; left:4px;height:475px;border-radius:8px; border:groove; width:280px">    
          <select id="book_selection" style="position:relative; top:10px; left:5px"> 
          <option id="1">Select Book</option> 
          <option id="2" selected>Quran</option>
          <option id="3">Sahi Bukhari</option>
          <option id="4">Sahi Muslim</option>
          <option id="5">Sahi Termzi</option>
          </select>  <!--Book selection ends -->
          <br />
          <input type="button" id="go" value="Go" style="position:relative; top:425px;left:115px;border-radius:5px" />
      </div> <!--left-container ends -->

      <div id="right-container" style="background-color:LightSteelBlue      ; position:absolute; top:65px; left:294px;height:475px;border-radius:8px; border:groove; width:936px">    

<script>


    alert("value of i is as bellow :" + 9);
    alert("disabled...");

</script>
      </div> <!--right-container ends -->


      </div> <!--outer div ends -->


</body>

</html>

2 个答案:

答案 0 :(得分:1)

您需要使用此类剃须刀标记

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
  }
<script>
    @{
        var i = (int)TempData["count"];
    }

    alert("value of i is as bellow :" + @i);

    @if (i == 9)
    {
        <text>
            $('#book_selection').attr("disabled", "disabled");
            alert("disabled...");
        </text>
    }
</script>

here您可以使用

找到Razor的一些基本规则

答案 1 :(得分:1)

虽然存在语法错误,但仍然正常工作:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
  }
<script>
    @{
        var i = TempData["message"];
    }

    alert("value of i is as bellow :" + @i);
   if (@i == "9")
    {
    $('#book_selection').attr("disabled", "disabled");
     alert("disabled.!");
    }
</scrip