-3

I need to save the user selected text to the db.I tried 3 different ways but all of them return null for the selected value. value1 or value2 or value3 returns null. What I am doing to wrong here ?

   <div id="reasonsList" style="display: none">
    @foreach (var reason in Model.CorrectionReasonsList)
    {
        <option>@(reason)</option>
    }
</div>
var value = '<option selected="selected" value="' + sData + '">' + sData + '</option>';

var reasonsSelect = '<select id="correction_reason_dropdown" ' +
         'multiple ' +
         'style="min-width:115px" ' +
         'data-select-options={"searchField":"false","noValueText":"Select One"))"} ' +
         'class="select multiple-as-single compact correction-reason" ' +
          '>';                

          reasonsSelect += value + $("#reasonsList").html();
           reasonsSelect += "</select>";
            $(nTd).html(reasonsSelect);


var value1 = $('#reasonsList:selected').val();
var value2 = $('#reasonsList:selected').find('option:selected').text();
var value3 = $('#reasonsList:selected').text();
Bhavik Patel
  • 1,466
  • 1
  • 12
  • 28
Everest
  • 37
  • 1
  • 11
  • Did you check [Get selected text from a drop-down list (select box) using jQuery](http://stackoverflow.com/questions/1643227/get-selected-text-from-a-drop-down-list-select-box-using-jquery) – Shyju Apr 13 '16 at 02:39
  • 1
    `#reasonsList` is a `
    `, not a `
    –  Apr 13 '16 at 02:39

1 Answers1

1

You need to get selected text from drop down not the div. Below code should do:

$("#correction_reason_dropdown:selected").text();

Cory
  • 1,263
  • 16
  • 31
Ramin
  • 197
  • 8
  • After looking closely at the code, it doesn't look right to nest the the option tag inside the div and then take its values to populate the drop down. Why not to create a select list items and use the @Html.DropDownListFor function like in here http://stackoverflow.com/questions/17727386/dropdownlist-in-mvc-4-with-razor – Ramin Apr 13 '16 at 03:16
  • Ramin, Thank you for looking through my code. It is a really complicated view . I only posted bare minim above. @Html.dropdown was my first choice too but it did not work. Then I tried to solve it with the help of javascript and jQuery. I guess this does not work either. – Everest Apr 13 '16 at 04:17
  • I changed the reasons select by adding the index of the row and it worked : var reasonsSelect = ' – Everest Apr 13 '16 at 19:36