user2756361
user2756361

Reputation: 41

Radio button selection event in MVC

I am a beginer ...I don't know how to write code during the radio button select change in MVC....I have used it like this

In csHTML page

@Html.RadioButtonFor(model=>Sales.Pay_Mode, true)Cheque
@Html.RadioButtonFor(model=>Sales.Pay_Mode, false)Cas

This is my cs page code....Where i want write the change event code and how i get the selected value in control page.My requirement is during radio button change i want change the style of the textbox enable as false..

@Html.TextBox("ChequeNo")

Upvotes: 2

Views: 13752

Answers (2)

Abbas Amiri
Abbas Amiri

Reputation: 3204

Considering the information that you have provided in your question, the following code would fulfill your expectation

@Html.RadioButtonFor(model => model.PayMode, "Cheque") Cheque 
@Html.RadioButtonFor(model => model.PayMode, "Cas") Cas

@Html.TextBox("ChequeNo", null, new {id="theTextBox"})

<script type="text/javascript">
    $("input[name='PayMode']").change(function () {
        var selectedRadio = $("input[name='PayMode']:checked").val();
        if (selectedRadio == 'Cas') {
            $("#theTextBox").attr("disabled", "disabled");
        } else {
            $("#theTextBox").removeAttr("disabled");
        }
    });
</script>

Upvotes: 3

maxs87
maxs87

Reputation: 2284

The is no control events handler in MVC like in WebForms or something like this. You must use javascript code to implement your logic. For example with jQuery

$('.your-radio-class').change(function(){
    $('#your-textbox-id').css('margin-left', '100px')
}); 

Upvotes: 0

Related Questions