Ajax 不工作并且控制台没有给出任何错误-MVC
Ajax not working and console not giving any error-MVC
我正在创建 Page.when 用户 在文本框中输入文本,它会将字符串传递给操作名称 "CheckRecord",即 PartialViewResult
然后 return 到主视图 "Create"。
创建视图:
<input type="text" id="enginNo" />
<input type="button" value="search" id="btnSearch" />
<div id ="result"></div>
<script type="text/javascript">
$(document).ready(function () {
$("#btnSearch").click(function () {
var enginNo = $('#enginNo').val();
$.ajax({
cashe :'false',
type: 'Get',
data: { enginNo: enginNo },
url: '@Url.Action("CheckRecord")',
dataType: 'HTML', // add this line
success: function (result) {
$('#result').html(result);
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
});
</script>
创建操作:
public ActionResult Create()
{
return View();
}
检查结果操作:
public PartialViewResult CheckRecord(string enginNo)
{
if(enginNo == null)
{
var StockDM = db.StockDMs.Where(c => c.EngineNumber == "54-21-31-AF-31");
return PartialView("_Part", StockDM.ToList());
}
else
{
var StockDM = db.StockDMs.Where(c => c.EngineNumber == enginNo);
return PartialView("_Part", StockDM.ToList());
}
}
局部视图“_Part”
@model IEnumerable<SM.CRM.AutosLoan.Models.Core.DomainModels.StockDM>
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.ChasisNumber)
}
The problem is that data is returning from database is perfectly but not loaded on div from ajax control...and also console is not giving
any errors...may be some stupid error...Please help,,,,Thanks for your
time
试试这个
$.ajax({
cashe :'false',
type: 'Get',
data: { enginNo: enginNo },
url: '@Url.Action("CheckRecord","ControllerName")',
dataType: 'HTML', // add this line
success: function (result) {
$('#result').html(result);
},
error: function (jqXHR, exception) {
............
}
});
在'@Url.Action("CheckRecord")'
中添加控制器名称
我正在创建 Page.when 用户 在文本框中输入文本,它会将字符串传递给操作名称 "CheckRecord",即 PartialViewResult 然后 return 到主视图 "Create"。 创建视图:
<input type="text" id="enginNo" />
<input type="button" value="search" id="btnSearch" />
<div id ="result"></div>
<script type="text/javascript">
$(document).ready(function () {
$("#btnSearch").click(function () {
var enginNo = $('#enginNo').val();
$.ajax({
cashe :'false',
type: 'Get',
data: { enginNo: enginNo },
url: '@Url.Action("CheckRecord")',
dataType: 'HTML', // add this line
success: function (result) {
$('#result').html(result);
},
error: function (jqXHR, exception) {
if (jqXHR.status === 0) {
alert('Not connect.\n Verify Network.');
} else if (jqXHR.status == 404) {
alert('Requested page not found. [404]');
} else if (jqXHR.status == 500) {
alert('Internal Server Error [500].');
} else if (exception === 'parsererror') {
alert('Requested JSON parse failed.');
} else if (exception === 'timeout') {
alert('Time out error.');
} else if (exception === 'abort') {
alert('Ajax request aborted.');
} else {
alert('Uncaught Error.\n' + jqXHR.responseText);
}
}
});
});
});
</script>
创建操作:
public ActionResult Create()
{
return View();
}
检查结果操作:
public PartialViewResult CheckRecord(string enginNo)
{
if(enginNo == null)
{
var StockDM = db.StockDMs.Where(c => c.EngineNumber == "54-21-31-AF-31");
return PartialView("_Part", StockDM.ToList());
}
else
{
var StockDM = db.StockDMs.Where(c => c.EngineNumber == enginNo);
return PartialView("_Part", StockDM.ToList());
}
}
局部视图“_Part”
@model IEnumerable<SM.CRM.AutosLoan.Models.Core.DomainModels.StockDM>
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.ChasisNumber)
}
The problem is that data is returning from database is perfectly but not loaded on div from ajax control...and also console is not giving any errors...may be some stupid error...Please help,,,,Thanks for your time
试试这个
$.ajax({
cashe :'false',
type: 'Get',
data: { enginNo: enginNo },
url: '@Url.Action("CheckRecord","ControllerName")',
dataType: 'HTML', // add this line
success: function (result) {
$('#result').html(result);
},
error: function (jqXHR, exception) {
............
}
});
在'@Url.Action("CheckRecord")'