2011-06-13 25 views
0

我想在選擇單選按鈕後立即更新表單上的其他控件。@ Html.RadioButtonFor回發

單擊單選按鈕後,如何讓這些控件調用控制器。單獨的提交(actionLink)按鈕不適用。

我的代碼是

<label for="ApplicationTypes_Single">On your own?</label> 
@Html.RadioButtonFor(x => x.ApplicationType, "Single", new { id = "ApplicationTypes_Single" }) 

<label for="ApplicationTypes_Joint">With someone else?</label> 
@Html.RadioButtonFor(x => x.ApplicationType, "Joint", new { id="ApplicationTypes_Joint" }) 

控制依賴於選擇

@if(Model.ApplicationType == AffordabilityPage1.ApplicationTypes.Joint) 
{ 
    @Html.LabelFor(model => model.SecondAge) 
    @Html.EditorFor(model => model.SecondAge) 
} 

對於給定的任何提示非常感謝。

回答

1

爲了達到此目的您需要使用javascript。因爲如果你正在使用jQuery例如,你可以訂閱這些單選按鈕的變化事件,並採取一些行動:

$(function() { 
    $(':radio[id^="ApplicationTypes"]').change(function() { 
     var value = $(this).val(); // will equal Single or Joint 
     // Depending on what you are trying to achieve either send an AJAX request 
     // or force the submission of some form in order to send the new value to the server 
    }); 
}); 
+0

我嘗試這樣做,果然它的作品,我仍然有很多東西需要學習怎麼樣去完成它但這是一個很好的開始。很確定我可以找到如何做到這一點的例子。知道這是正確的道路就是進步。非常感謝。 – Gemma 2011-06-14 10:10:27