Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/hide control based on dropdown selection mvc 4 razor c#

Here is my code

@Html.DropDownListFor(z => z.SelectedReportId, new SelectList(Model.ReportTypes, "Value", "Text", Model.SelectedReportId), "-- Select Report --")
@Html.CheckBoxFor(model => model.IncludePhotos)@Html.LabelFor(model => model.IncludePhotos)

Which generates:

<select data-val="true" data-val-number="The field SelectedReportId must be a number." data-val-required="The SelectedReportId field is required." id="SelectedReportId" name="SelectedReportId">
    <option value="">-- Select Report --</option>
    <option value="1">Excel Report</option>
    <option value="2">Text Report</option>
</select>
<br />
<input data-val="true" data-val-required="The Include photos in the report field is required." id="IncludePhotos" name="IncludePhotos" type="checkbox" value="true" />

I have one dropdown and a checkbox, I need to disable the checkbox if the user selects the first value in dropdown. Here is the javascript I am using without success

$(function () {
    $('#SelectedReportId').change(function () {
        var value = $(this).val();
        if (value == '1') {
            $('#IncludePhotos').show();
        } else {
            $('#IncludePhotos').hide();
        }
    });
});

Appreciate any help, thank you

like image 749
Steve Ed Avatar asked Feb 18 '15 17:02

Steve Ed


1 Answers

Included javascript inside a @section scripts{} section and it started working,

@section scripts{ <script type="text/javascript">
$(function () {
    $('#SelectedReportId').change(function () {
        var value = $(this).val();
        if (value == '1') {
            $('#IncludePhotos').show();
        } else {
            $('#IncludePhotos').hide();
        }
    });
});</script>}
like image 193
Steve Ed Avatar answered Oct 20 '22 00:10

Steve Ed