Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC - dropdownlist onchange never fired in jquery

I am struggling to get dropdown list onchange event in jquery. When selecting Date from dropdownlist, other textbox should be invisible and date textbox shoud be visible. And when selecting Status, other textboxes should be invisible and status of dropdownlist should be visible. It happens nothing. Please look at my code what i am doing wrong. Your help means alot.

_Layout.cshtml

 <head>
  <script type="text/javascript">
    $(function () {
        $('#categorie').on('change', function () {
            if ($(this).val() == "Date") {
                $('#keyword').hide(); //invisible
                $('#txtcalendar').show();
            } else if ($(this).val() == "Status"){
                $('#keyword').hide(); //invisible
                $('#txtcalendar ').hide();
                $('#tmstatus ').show();
            }
            .
            .
            .
        });

    });
    </script> 
</head>

Index.cshtml

@Html.DropDownList("categorie", new SelectList(new[]
                                                   {
                                                       "All", "Id", "Status",
                                                       "Vendor", "Date"
                                                   }) as SelectList)



 <p> Keyword: @Html.TextBox("keyword")  <input id="Submit" type="submit" value="Search"  /> </p>

 <p>Calendar @Html.TextBox("txtcalendar", new {}, new { @class = "myclass",  style = "display:none;" })</p>

 @Html.DropDownList("tmstatus", new SelectList(new[]
                                                   {
                                                       "Success", "Pending", "Error",
                                                   }) as SelectList)
like image 854
user2857908 Avatar asked Dec 31 '25 20:12

user2857908


1 Answers

In your application, Go to app_start\BundleConfig.cs and in RegisterBundles method check that bundling for Jquery has been registered or not. It should have added bundle for jquery as mentioned below :

bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));

If it is not there in RegisterBundles method then add it. And go to below steps :

  1. Go to your _Layout.cshtml page

  2. Add @Scripts.Render("~/bundles/jquery") before end of the head tag. In my sample application it looks like as mentioned below:

    enter image description here

  3. Cut your javascript code of dropdownlist change from _Layout.cshtml

  4. Paste it in index.cshtml at the end.

My Index.cshtml page looks like this :

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index Page</h2>

@Html.DropDownList("categorie", new SelectList(new[]
                                                   {
                                                       "All", "Id", "Status",
                                                       "Vendor", "Date"
                                                   }) as SelectList)



<p> Keyword: @Html.TextBox("keyword")  <input id="Submit" type="submit" value="Search" /> </p>

<p>Calendar @Html.TextBox("txtcalendar", new { }, new { @class = "myclass", style = "display:none;" })</p>

@Html.DropDownList("tmstatus", new SelectList(new[]
                                                   {
                                                       "Success", "Pending", "Error",
                                                   }) as SelectList)

<script type="text/javascript">
    $(function () {
        $('#categorie').on('change', function () {
            if ($(this).val() == "Date") {
                $('#tmstatus ').show();
                $('#keyword').hide(); //invisible
                $('#txtcalendar').show();
            } else if ($(this).val() == "Status") {
                $('#keyword').hide(); //invisible
                $('#txtcalendar ').hide();
                $('#tmstatus ').show();
            }
        });

    });
</script>
like image 141
SpiderCode Avatar answered Jan 03 '26 12:01

SpiderCode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!