Lex Webb
Lex Webb

Reputation: 2852

MVC DropDownList onchange event with value

I'm pretty new to MVC and i'm trying to determine the value of a DropDownList when the value is changed. At the moment the val variable being passed to my javascript function is always 'undefined'. I've googled for the solution and everywhere says to do exactly what i'm doing right now. What am i doing wrong?

cshtml:

@Html.DropDownListFor(m => m.NearestDealers
     , new SelectListItem[] 
     { new SelectListItem() {
               Text = "No nearest dealers", Value = "0"
          },
          new SelectListItem() {
               Text = "1", Value = "1"
          },
          new SelectListItem() {
               Text = "2", Value = "2"
          },
          new SelectListItem() {
               Text = "3", Value = "3"
          } 
      }, new { onchange = "onNearestDealersChange(this.Value);"})

And my javascript:

function onNearestDealersChange(val) {
    if (val == 0) {
        $("#dealer-pools").addClass("hidden");
    } else {
        $("#dealer-pools").removeClass("hidden");
    }
}

Upvotes: 1

Views: 2394

Answers (2)

Truong Mai Van
Truong Mai Van

Reputation: 147

                <td>
                    @Html.DropDownListFor(modelItem => item.donvitinh
                             , new SelectListItem[]
                             {
                                // new SelectListItem() {
                                 //      Text = "No nearest dealers", Value = "0"
                                 // },
                                  new SelectListItem() {
                                       Text = "CS", Value = "CS"
                                  },
                                  new SelectListItem() {
                                       Text = "EA", Value = "EA"
                                  },

                              }, new
                                   {


                                  id = @item.id,

                                  onkeydown = "return  event.keyCode != 13",
                                  onchange = "replaceContentsOfDivupdatedonvi(this.id,this.value);"
                              })

                    @Html.ValidationMessageFor(modelItem => item.donvitinh)

                </td>

May be it help some one

<script>
function replaceContentsOfDivupdatesoluong(iditem, soluong) {
    $.ajax({
        url: '@Url.Action("AddPaialTransferUpdatesoluong", "Home")',
        data: {
            id: "_DetailTransferlist.cshtml",
            soluong: soluong,
            iditem: iditem
        },
        type: "POST",
        success: function(data) {
            $('#placeHolderDiv2').html(data);
        }
    });
}

Upvotes: 0

Kialandei
Kialandei

Reputation: 394

Try using a lower case 'v' for 'this.Value'

new { onchange = "onNearestDealersChange(this.value);"}

Upvotes: 2

Related Questions