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
}