Tuesday, 7 February 2017

MVC - jQuery - Get text and value of selected item in DropDownList


Watch on YouTube


@using (Html.BeginForm(null, null, FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @Html.DropDownListFor(Model =>Model.CompanyID, new SelectList(Model.Comp, "CompanyID", "CompanyName"), "Default", new { @id ="CopmID"})
    <input type="submit" value="Save" />
}

@section Scripts{
    <script type="text/javascript">
        $(document).ready(function () {
            $("#CopmID").change(function (evt) {
                alert($("#CopmID").val()); //Value
                alert($("#CopmID option:selected").text());  //Text

            });
        });
    </script>

    }

1 comment: