I have a form with a single text box and a button, once the button is pressed the value entered in the textbox is then passed to my index method that runs a linq query. the results are then to be displayed on a modal.
I'm new to ajax but after looking at several other posts Ive come up with the below, my problem is that the ajax runs and the modal opens but the data has not been populated. Can anyone see where I'm going wrong?
Here is my code:
Form:
<form id="getDataForm" action="/Home/Index" method="get" role="form">
<label for="platform" class="control-label">Enter Code, IMEI or Phone Number:</label><br />
<div class="inner-addon left-addon">
<i class="glyphicon glyphicon-qrcode"></i>
<input id="filtername" name="filtername" type="text" class="form-control" value="" placeholder="Please Enter Code" data-toggle="popover" data-original-title="Larger Tool tip."/>
</div>
<input id="btnGetData" class="btn btn-success" value="GO" />
</form>
Home/Index method:
[HttpGet]
public ActionResult Index(string filtername)
{
var filterresults = from m in db.UserInfoes select m;
filterresults = filterresults.Where(x => x.UserCode.ToString().Contains(filtername)).OrderBy(x => x.UserCode);
MasterModel model = new MasterModel();
model.UserInfo = filterresults;
return CheckUserLogged(model);
}
JQuery:
$("#btnGetData").click(function () {
$.ajax({
type: "GET",
url: "/Home/Index",
data: $('#getDataForm').serialize(),
datatype: "html",
success: function (data) {
$('#basicModal2').modal('show');
console.log("yaya");
}
});
});
Edit Added Modal:
@* Existing Request - Date Selection Modal *@
<div class="modal fade" id="basicModal2" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
<h4 class="modal-title" id="myModalLabel">Summary</h4>
</div>
<div class="modal-body">
<h2>Results</h2>
<span id="printCode"></span><br />
<div class="pull-right"><button type="submit" class="btn btn-success" id="toggle">Toggle</button> </div>
<script>
$(document).ready(function () {
$(".checks").hide()
});
</script>
<table class="table">
<thead>
<tr>
<th></th>
<th>Date</th>
<th>Diagnostic Mode</th>
</tr>
</thead>
<tbody>
@if (Model != null)
{
foreach (var item in Model.UserInfo)
{
<tr>
<td>
<input type="checkbox" class="checks">
</td>
<td class="modal2row" data-toggle="modal" data-id="1" data-target="#basicModal3">
@Html.DisplayFor(modelItem => item.CreationDateTime)
</td>
<td class="modal2row" data-toggle="modal" data-id="1" data-target="#basicModal3">
@Html.DisplayFor(modelItem => item.AppModeId)
</td>
</tr>
}
}
</tbody>
</table>
Usefull info:
I followed this which allowed me to see the value being called as it should:
Go to the web page which makes the AJAX call
In Chrome press F12
Go to the Network tab
Activate the AJAX call by submitting the form #reservSearch
In the Network tab look for a call to /Home/GetRates
Click it
Check the Preview and Response tabs to see the output from your server
Is it displaying the expected HTML data which your AJAX call is listening for?
http://stackoverflow.com/questions/21533285/why-the-ajax-script-is-not-running-on-iis-7-5-win-2008-r2-server/21617685#21617685
The output in my case:
Request: GET /Home/Index?filtername=3it60nhrha HTTP/1.1 (filtername=3it60nhrha is expected)
Response HTTP/1.1 200 OK