0

I have tried 5 different ways to get selected dropdown value but all fails.

// JourneyType: $("#JourneyType").find("option:selected").text(),
// JourneyType: $("#JourneyType :selected").text(),
//  JourneyType: $('#JourneyType option:selected').text(),
//   JourneyType: $("[id='JourneyType'] :selected"),

This journey type object is in a json array named (sof) where i want to set selected dropdown value.The json array with ajax function is something like this:

             <script>
                $(document).ready(function () {                 
                    $("#btnPost").click(function () {
                     var sof = {
                            EndUserIp: "192.168.10.10",
                            TokenId: $("#pageInitCounter").val(),
                            AdultCount: $("#AdultCount").val(),
                            ChildCount: $("#ChildCount").val(),
                            JourneyType: $("#JourneyType :selected").text(),                                          
                        }
                        $.ajax(
                            {
                                url: "/api/Flight/SearchFlight",
                                type: "Post",
                                contentType: "application/json",
                                data: JSON.stringify(sof),
                                success: function (result) {
                                    alert(result);
                                }
                            });        
                    });
                });
        </script>

The simple dropdown from which i want to get selected text value:

 <div class="form-group">
                        <label class="control-label col-md-2" for="DepartmentID">Journey Type</label>
                        <div class="col-md-10">
                            @Html.DropDownListFor(model => model.JourneyType, Model.JourneyList)
                            @Html.ValidationMessageFor(model => model.JourneyType, "", new { @class = "text-danger" })
                        </div>
                    </div>

And the model from where these values for dropdown are coming is like this:

   public enum JourneyType
{
    OneWay, Return, MultiStop, AdvanceSearch, SpecialReturn
}

public class SearchForFlight
{
    public SearchForFlight()
    {
        JourneyList = new List<SelectListItem>();
    }
    public string EndUserIp { get; set; }
    public string TokenId { get; set; }

    public IEnumerable<SelectListItem> JourneyList { get; set; }

    public Enum JourneyType { get; set; }
}

I am using Web api Controller action method like this but everytime i am seeing null in JourneyType of sof object:

  public async Task<HttpResponseMessage> SearchFlight([FromBody]SearchForFlight sof)
     {
 // implementation goew here
      }
duke
  • 1,816
  • 2
  • 18
  • 32
  • What did u get when u run this, JourneyType: $("#JourneyType :selected").text(), – sakir May 13 '16 at 12:29
  • i can see null only @sakir – duke May 13 '16 at 12:32
  • try this one , @Html.DropDownListFor(model => model.JourneyType, new SelectList(Model.JourneyList, "key", "value",new { @class = "form-control"}) – sakir May 13 '16 at 12:37
  • DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Key' error – duke May 13 '16 at 12:46
  • u can try using SelectList, – sakir May 13 '16 at 12:47
  • in that way, its now showing any option only System.web.Mvc.SelectListItem is shown five times istead of options. as u said i tried this @Html.DropDownListFor(model => model.JourneyType, new SelectList(Model.JourneyList)) – duke May 13 '16 at 12:53
  • dont forget add this one too new { @class = "form-control"}) – sakir May 13 '16 at 12:54
  • ok just try this one @Html.DropDownListFor(model => model.JourneyType, Model.JourneyList,new { @class = "form-control"}) – sakir May 13 '16 at 12:56
  • still null thnks for ur try @sakir – duke May 13 '16 at 13:00
  • Have you tried this: @Html.DropDownListFor(m => m.JourneyType, new SelectList(Model.JourneyList, "Value" , "Text") - then do $('#JourneyType').val() – Dawood Awan May 13 '16 at 13:04
  • @duke: Could you tell the console output for this code `$("#JourneyType")` first? – Amol M Kulkarni May 13 '16 at 14:40
  • @AmolMKulkarni I have tried this JourneyType: $("#JourneyType :selected").text() the console output is showing correct value selected however i think here value got manipulated – duke May 16 '16 at 06:27

1 Answers1

0

Add html id for your dropdownlist

@Html.DropDownListFor(model => model.JourneyType, Model.JourneyList, new {id = "JourneyType"} )
Flowerking
  • 2,551
  • 1
  • 20
  • 30
  • "{\"EndUserIp\":\"192.168.10.10\",\"TokenId\":\"37f5c042-6482-4492-a5e5-f0e88ccd1a4b\", \"AdultCount\":2,\"ChildCount\":0,\"InfantCount\":0,\"DirectFlight\":false, \"OneStopFlight\":false,\"JourneyType\":null,\"PreferedLines\":null, \"Origin\":null,\"Destination\":null,\"FlightCabinClass\":null,\"PreferredDepartureTime\":\"\", \"PreferredArrivalTime\":\"0001-01-01T00:00:00\",\"Sources\":null}" this is the serialized json returned back – duke May 13 '16 at 13:27
  • I have other properties but in ques i have omitted them – duke May 13 '16 at 13:27
  • 1
    I think it's an issue with using ENUM type for the dropdown, please see http://stackoverflow.com/questions/18380576/bind-enum-with-dropdown-and-set-selected-value-on-get-action-in-mvc-c-sharp – Flowerking May 13 '16 at 13:28
  • I also think so that all this is happening due to Enum but anyways 3 hours are wasted and nothing achieved at the end @Flowerking – duke May 13 '16 at 13:32
  • @Flowerking no it isn't - Enums don't cause any issue – Dawood Awan May 13 '16 at 13:41
  • @duke Just a debugging hint, is it still `null` when you run `$("#JourneyType :selected").text()` in browser console after adding the `id` as I suggested! – Flowerking May 13 '16 at 13:47