2016-05-13 42 views
0

我已經嘗試了5種不同的方式來獲取選定的下拉值,但都失敗了。如何獲得下拉選擇在json數組中的值

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

這旅程類型的對象是在(SOF)命名爲JSON陣列,其中我想設置使用Ajax功能選擇下拉價值。JSON陣列是這樣的:

   <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> 

簡單下拉從中我想選擇的文本值:

<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> 

又從哪裏爲下拉這些值將未來的模式是這樣的:

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

我使用的Web API控制器的操作方法是這樣,但每次我在SOF對象JourneyType看空:

public async Task<HttpResponseMessage> SearchFlight([FromBody]SearchForFlight sof) 
    { 
// implementation goew here 
     } 
+0

當你運行這個時,你得到了什麼,JourneyType:$(「#JourneyType:selected」)。text(), – sakir

+0

我只能看到null @sakir – duke

+0

嘗試th是一個,@ Html.DropDownListFor(model => model.JourneyType,new SelectList(Model.JourneyList,「key」,「value」,new {@class =「form-control」}) – sakir

回答

0
var text = $("#JourneyType").text(); 
+0

已經嘗試和我必須得到文本值 – duke

+0

如果你想要文本值,然後使用$(「JourneyType」)。 –

+0

請給出一些描述 –

0

添加HTML iddropdownlist

@Html.DropDownListFor(model => model.JourneyType, Model.JourneyList, new {id = "JourneyType"}) 
+0

嘗試返回空值 – duke

+0

「{\」EndUserIp \「:\」192.168.10.10 \「,\」TokenId \「:\」37f5c042-6482-4492-a5e5-f0e88ccd1a4b \「, \ AdultCount \「:2,\」ChildCount \「:0,\」InfantCount \「:0,\」DirectFlight \「:false, \」OneStopFlight \「:false,\」JourneyType \「:null \ \「:\」null,\「\」\「,」 \「PreferredArrivalTime \」:\「\」:\「Origin \」:null,\「Destination \」:null,\「FlightCabinClass \」:null \「PreferredDerivateTime \ 0001-01-01T00:00:00 \「,\」Sources \「:null}」這是序列化的json返回 – duke

+0

我有其他屬性,但在問題中,我省略了它們 – duke

相關問題