2012-03-11 56 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