我已經嘗試了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
}
當你運行這個時,你得到了什麼,JourneyType:$(「#JourneyType:selected」)。text(), – sakir
我只能看到null @sakir – duke
嘗試th是一個,@ Html.DropDownListFor(model => model.JourneyType,new SelectList(Model.JourneyList,「key」,「value」,new {@class =「form-control」}) – sakir