I am using ajax to send values from my view to a controller in my application.
Ajax:
<script type="text/javascript">
$('#SendToController').on('click', function() {
sendToController();
return false;
});
function sendToController(){
var selectedM = $('#Month').val();
var chosenY = $('#Year').val();
var chosenC = $('#County').val();
//alert(chosenY);
$.ajax({
url: '/ALog/MReports/Generate',
data: { 'monthV' : selectedM, 'year' : chosenY, 'county' : chosenC },
type: 'GET',
cache: false,
success: function (data) {
$("body").html(data);
$('.dropdown-toggle').dropdown();
},
error: function () {
if (selectedM === "") {
alert("Error 1!");
}
else if (chosenY === "") {
alert("Error 2!");
}
else if (chosenC === "") {
alert("Error 3!");
}
}
});
}
</script>
Now, on my form i have 3 dropdownlists which I am getting the values from in the ajax code above. The only time the ajax function goes to the error
part is if chosenC
and/or selectedM
is empty, but if chosenY
is empty it still goes to success
.
I have alerted the value of chosenY
when I left it blank to ensure that I am checking for the right value with javascript, either null
or empty (""
).. I have tried both, and neither work..
Here is the result from when I alerted the chosenY
so from that I thought it was an empty string.. so I am checking for that.. but it doesn't matter because even if it is empty the ajax method is considering that as a success
and not an error
.. why is that?
Any help is appreciated.