2012-03-11 54 views
0

下面的代码工作:MVC 3条件HTML

@if (Model.document.usesNumericRevision) 
      { 
      <tr> 
        <th>Major Revision</th> 
        <td>@Html.TextBoxFor(m => m.document.documentStatus.documentRevision.MajorRevision)</td> 
      </tr> 
      <tr> 
        <th>Minor Revision</th> 
        <td>@Html.TextBoxFor(m => m.document.documentStatus.documentRevision.MinorRevision)</td> 
       </tr> 
      } 
     else 
      { 
      <tr> 
       <th>Alphanumeric Revision</th> 
       <td>@Html.TextBoxFor(m => m.document.documentStatus.documentRevision.Revision)</td> 
      </tr> 
      } 

根据usesNumericRevision的价值相应的HTML显示。但是,如果用户选中/取消选中复选框,则视图将不会更新。有没有办法更新视图,而无需返回到服务器?

回答

0

视图中的条件语句在服务器上执行并发送给客户端。您需要使用JavaScript来完成客户端,而无需返回服务器。

0

是的......但你需要一些jQuery做客户端的东西。

<div id="NumericRevision"> 
<tr> 
    <th>Major Revision</th> 
    <td>@Html.TextBoxFor(m => m.document.documentStatus.documentRevision.MajorRevision)</td> 
</tr> 
<tr> 
    <th>Minor Revision</th> 
    <td>@Html.TextBoxFor(m => m.document.documentStatus.documentRevision.MinorRevision)</td> 
</tr> 
</div> 
<div id="AlphanumRevision"> 
    <tr> 
     <th>Alphanumeric Revision</th> 
     <td>@Html.TextBoxFor(m => m.document.documentStatus.documentRevision.Revision)</td> 
    </tr> 
</div> 

...

<script> 
$(document).ready(function() { 
    //Initial state of the form 
    $("#AlphanumRevision").css("display","none"); 

    // Add onclick handler to checkbox w/id "checkme" 
    $("#checkme").click(function(){ 
     // If checked 
     if ($("#checkme").is(":checked")){ 
     $("#AlphanumRevision").show("fast"); 
      $("#NumericRevision").hide("fast"); 
     } 
     else { 
     $("#AlphanumRevision").hide("fast"); 
      $("#NumericRevision").show("fast"); 
    } 
    }); 
}); 
</script> 
+0

谢谢你开启我的jQuery。我想我恋爱了。以下是我的代码: – 2012-03-11 21:14:22

+0

2012-03-11 21:15:22