1

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");
    }
}

2 Answers 2

2

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

new { onchange = "onNearestDealersChange(this.value);"}
Sign up to request clarification or add additional context in comments.

Comments

0
                <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);
        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.