Jquery ASP.net mvc中未使用ajax将数据加载到表中
我想在ASP.net mvc中使用Ajax检索html表中的数据,但成功部分没有执行,并且不知道如何使用Ajax在表中显示返回的数据。请提出解决此问题的任何方法。谢谢 Index.cshtmlJquery ASP.net mvc中未使用ajax将数据加载到表中,jquery,asp.net,ajax,asp.net-mvc,asp.net-mvc-3,Jquery,Asp.net,Ajax,Asp.net Mvc,Asp.net Mvc 3,我想在ASP.net mvc中使用Ajax检索html表中的数据,但成功部分没有执行,并且不知道如何使用Ajax在表中显示返回的数据。请提出解决此问题的任何方法。谢谢 Index.cshtml @model IEnumerable<MvcApplication4.Models.tbl_product> @{ Layout = null; } <!DOCTYPE html> <html> <head> <script src="
@model IEnumerable<MvcApplication4.Models.tbl_product>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
<title>Index</title>
<script type="text/javascript">
$(document).ready(function () {
$('#Button1').click(function () {
alert("button clicked");
$.ajax({
type: 'POST',
contentType: "application/json; charset=utf-8",
url: 'Home/Index',
data: "{'searchString':'" + document.getElementById('searchString').value + "'}",
async: false,
Success: function (response) {
alert("Success");
window.location.reload();
},
error: function () { alert("error"); }
});
});
});
</script>
</head>
<body>
@Html.TextBox("searchString");
<input type="button" value="filter" id="Button1" />
<table id="showData">
@{Html.RenderPartial("SearchList");}
</table>
</body>
</html>
SearchList.cshtml
@foreach (var item in Model)
{
<tr>
<td>@item.ProductName</td>
<td>@item.ProductId</td>
<td>@item.ProductDesc</td>
</tr>
}
@foreach(模型中的变量项)
{
@item.ProductName
@item.ProductId
@item.ProductDesc
}
成功
不是成功
表
HTMLsuccess: function (response) {
alert("Success");
$('#showData').html(response)
},
控制器代码
return PartialView("SearchList", query.ToList());
如果您不提供ViewName
,按照惯例,它将使用ActionName
作为视图名称。因此,将SearchList
传递为ViewName
编辑:
此外,还需要传递模型以渲染局部
@{Html.RenderPartial("SearchList", Model);}
首先,它是
success
而不是success
它抛出了什么错误。它是否在controllerand中命中操作,还包括数据类型:'json'代码>在您的ajax调用中更改为小写后,现在正在执行成功部分,但是如何将数据搜索附加到表中,以及如何在OnBlur事件上调用ajax函数,而不是单击按钮。。。感谢以上建议…更改代码,它正在工作,但当我点击过滤器按钮时,会生成另一个文本框。为什么会这样。。谢谢..尝试返回PartialView(“SearchList”,query.ToList())代码>。您需要使用搜索列表
局部视图
@{Html.RenderPartial("SearchList", Model);}